0

아래 코드를 고려하여 uniqueAttribute 조건을 충족하는 경우 ng-model을 두 개의 다른 개체로 동적으로 구성하려고합니다. 이 같은 처리 할 수있는 방법이 있나요 또는함수를 할당하고 모델을 동적으로 생성하는 방법이 있습니까?

Error: [ngModel:nonassign] Expression 'vm.isUniqueAttribute(entityDefinition)' is non-assignable.

: 아래

<input type="text" class="form-control" ng-model="vm.isUniqueAttribute(entityDefinition)" required /> 

vm.abc 또는 vm.defng-model

  vm.isUniqueAttribute = function(entityDef) { 
       return entityDef.isUnique === 'true' ? 'vm.abc': 'vm.def'; 
      } 

에 바인딩 반환하는 기능입니다 그러나 같은 오류가 발생합니다 이것을 달성하기위한 다른 방법은 없습니까?

나는 하나의 객체를 할당하고 나중에 두 개의 다른 객체로 최종 옵션을 분류하여 할 수 있습니다. 그러나 많은 노력 없이도 처리 할 수 ​​있는지 궁금합니다.

+0

코드가 제대로 작동해야합니다. 'entityDefinition' (매개 변수)이 정의 되었습니까? 문제는 매개 변수 내에서만 발생합니다. – SaiUnique

+0

은 ng-model에 기능을 할당 할 수 없습니다. – Mithun

+0

매개 변수 자체를 사용하여 빌드해야합니다. true이면 true 객체 또는 falseobject에 바인딩합니다. – Mithun

답변

0

두 개의 다른 요소를 두 개의 모델로 사용하고 ng-if를 사용하여 플래그를 기반으로 필요한 요소를 토글합니다.

<input type="text" class="form-control" ng-model="vm.abc" ng-if="vm.isUniqueAttribute(entityDefinition)" required /> 
<input type="text" class="form-control" ng-model="vm.def" ng-if="!vm.isUniqueAttribute(entityDefinition)"required /> 
+0

에서 전달되는 데이터가이 작업을 수행 할 수 있습니다. 그러나이 텍스트 필드가 루프에서 형성된다는 제약이 있으므로이 방법을 사용하면 항상 2 개의 입력 필드를 갖게됩니다. 그리고 10이면 두 배입니다. – Mithun

0

당신은 object (vm.modelCollection)의 속성으로 당신의 ng-model을 설정하고 isUniqueAttribute 방법으로 주어진 속성의 이름을 반환하는 브라켓 표기법을 사용할 수 있습니다.

<input type="text" class="form-control" ng-model="vm.modelCollection[vm.isUniqueAttribute(entityDefinition)]" required /> 

다음

vm.isUniqueAttribute = function(entityDef) { 
    return entityDef.isUnique === 'true' ? 'abc': 'def'; 
} 

이 방법은 사용자의 요구

0
I 동적 콘텐츠와 정적 모델을 활용하여 제안

맞는 경우 나도 몰라. 동적 데이터를 제공하기 위해 샘플에서 "ng-init"을 사용했지만 필요에 따라 컨트롤러에 의존 할 수는 있습니다. 샘플 코드가

(여기 https://docs.angularjs.org/api/ng/directive/ngInit 상태로) :

$scope.Model = []; 
$scope.SetModelWithDynamicContent = function(index) { 
if ($scope.Model.indexOf(index) == -1) { 
    //Add your conidition here(.isunique). I have used odd or even number. 
    if (index % 2 == 0) 
    $scope.Model.push({ 
     Property: "Bind vm.abc" 
    }); 
    else 
    $scope.Model.push({ 
     Property: "Bind vm.def" 
    }); 
} 

그것의 전체 코드가 http://plnkr.co/edit/gFa26r?p=preview

에서 찾을 수 있습니다 그것은 당신의 문제가 해결되는지 답변으로이를 표시하기 위해 기억하십시오!

관련 문제