2012-10-11 23 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
  1. 你不应该在ng-model使用{{ }},你应该写:ng-model="publisherParam.label"
  2. 不能输入的名称中使用表达式,它需要一个静态的字符串,支持现场级别验证。

如果你可以共享一个jsFiddle我很乐意帮助更多的实际代码。

+0

其实我需要的东西像NG-模型= “publisherParam1” 或类似的东西,但如果我添加其他输入动态再我需要ng-model =“publisherParam2”..并且我无法找到如何添加索引或某些东西来区分ng-models – Mythriel

0

听起来好像你想要使用ng-repeat和$ index来获取要放入表单输入字段名称的项目索引。我不确定你想要做什么,但希望这会让你在正确的方向...

编辑:此外,输入的名称=“”是无关当在Angular中提交表单,因为您可以将整个对象改为$ 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

我只是想在任何角度问题上做出贡献。我喜欢这个框架,而且我希望它能够比现有的更多一点。 –

相关问题