2

我使用路由将不同的模板加载到 ngView 中。其中一个模板有一个简单的控制器,其中包含一组联系人。我在这里尝试做的很简单,只需单击一个链接 (ngclick) 调用一个函数并将一个新对象添加到我希望在我的 UI 中反映的数组中。

是这样的:

$scope.contacts = [{name='', email=''}];

<li ng-repeat="con in contacts">
  <input type="text" ng-model="con.name"/>
  <input type="email" ng-model="con.email"/> 
</li>
<li>
  <a href ng-click="addContact()">add</a>
</li>

$scope.addContact = function() {
  $scope.contacts.push( {name='', email=''} ); //-- i can use either $scope or this to reference the array and works.
}

因此,UI 以初始值呈现良好,点击时调用 addContact 函数,我看到值被推送(长度 = 2),但函数结束后,数组似乎重置为一个元素(长度 = 1) angularjs 评估。

我不确定这是否发生,因为我使用 ngView。我的意思是,我查看了这个示例(http://code.angularjs.org/1.0.3/docs/api/ng.directive:ngController),我没有看到我在这里尝试做的事情有什么不同,不同之处在于我将路由与 ngView 一起使用。

小提琴:http: //jsfiddle.net/fdDph/

非常感谢您的帮助。

4

2 回答 2

3

在您的小提琴中,您在 ng-show 中将数组长度重置为 1:

<span ng-hide="contacts.length = 1">

这样做,它将起作用:

<span ng-hide="contacts.length == 1">
于 2012-12-19T04:32:24.603 回答
0

{name='', email=''}顺便说一句,语法错误,应该是{name:'', email:''}

于 2012-12-18T19:28:53.150 回答