2012-10-11 4 views
1

왜 이것이 AngularJS에서 작동하지 않습니까?입력에서 동적으로 추가 할 때 각도가 내 객체를 평가하지 않습니다.

<div class="inputbox" ng-repeat="publisherParam in publisherParams"> 
    <div> 
     <label>{{ publisherParam.label }}</label> 
    </div> 
    <div> 
     <input type="text" name="{{ publisherParam.label }}" ng-model="{{ publisherParam.label }}" required /> 
    </div> 
</div> 
<div class="submitbox"> 
    <button class="purple-btn" ng-disabled="publisherForm.$invalid"> 
     Add Network 
    </button> 
</div> 

답변

3
    당신은 ng-model{{ }}를 사용해서는 안
  1. , 당신은 작성해야 : 당신은 입력의 이름으로 표현을 사용할 수 없습니다 ng-model="publisherParam.label"
  2. , 그것은 현장 지원하기 위해 정적 문자열이 될 필요가있다 레벨 검증.

jsFiddle을 공유 할 수 있다면 기꺼이 실제 코드를 더 유용하게 사용할 수 있습니다.

+0

사실 내가 NG 모델 = "publisherParam1"나 뭐 그런 식으로 뭔가를해야하지만 그때 동적 다른 입력을 추가하는 경우 나는 ng-model = "publisherParam2"가 필요합니다. 그리고 인덱스를 추가하는 방법이나 ng 모델을 구별하는 방법을 찾을 수 없습니다 – Mythriel

0

ng-repeat 및 $ index를 사용하여 양식 입력란의 이름에 넣을 항목의 색인을 얻으려는 것 같습니다. 정확히 무엇을하려고하는지 잘 모르겠지만 잘하면 올바른 방향으로 가게됩니다.

EDIT : 또한 입력의 이름 = ""은 각도를 사용하여 양식을 제출할 때 부적합합니다. , 당신은 $ scope에서 변경하는 전체 객체를 가져 와서 AJAX를 통해 보낼 수 있습니다.

HTML

<form name="publisherForm" ng-submit="formSubmit()"> 
    <div class="inputbox" ng-repeat="publisherParam in publisherParams"> 
     <div> 
      <label>{{publisherParam.label}}</label> 
     </div> 
     <div> 
      <!-- use $index to get the index of the item in the array --> 
      <!-- note, no {{}} inside of the ng-model tag. 
       That's being $eval'ed so no {{}} needed --> 
      <input type="text" name="publisherParam_label_{{$index}}" ng-model="publisherParam.label" required /> 
     </div> 
    </div> 
    <!-- just an add button so you can see it work --> 
    <button type="button" ng-click="addPublisherParam()">Add</button> 
    <!-- your submit button --> 
    <div class="submitbox"> 
     <button class="purple-btn" type="submit ng-disabled="publisherForm.$invalid"> 
      Add Network 
     </button> 
    </div> 
</form> 

JS

app.controller('YourController', function($scope) { 
    //this is the array you're displaying 
    $scope.publisherParams = [ 
     { label: 'First Label' }, 
     { label: 'Second Label' } 
    ]; 

    //just an indexer 
    var p = 0; 
    $scope.addPublisherParam = function(){ 
     //add a new publisher param. 
     $scope.publisherParams.push({ label: 'New Label ' + p++ }); 
    }; 

    $scope.formSubmit = function(){ 
     //do something here. 
    }; 
}); 
+0

입력 이름이 게시 데이터/m에 대해 중요하지는 않지만 사실입니다. odel 바인딩, 필드 이름 실제로 유효성 검사에 사용됩니다 그리고 이것이 내가 언급 한 이유입니다. –

+0

이것은 사실입니다. 개별 필드 레벨 유효성 검증 디스플레이는 까다 롭습니다. –

+0

나는 가능한 모든 각도 질문에 공헌하려고 노력하고있다. 이 프레임 워크가 마음에 듭니다. 나는이 프레임 워크를 조금 벗어나고 싶습니다. –

관련 문제