angularjs - 在 ngView 中动态添加新的表单字段 - Angularjs

标签 angularjs

我使用路由将不同的模板加载到 ngView 中。其中一个模板有一个简单的 Controller ,其中包含一组触点。我在这里尝试做的事情很简单,只需单击链接 (ngclick) 调用函数并将新对象添加到数组中,我希望该对象将反射(reflect)在我的 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 一起使用。

fiddle :http://jsfiddle.net/fdDph/

非常感谢您的帮助。

最佳答案

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

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

这样做就会起作用:

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

关于angularjs - 在 ngView 中动态添加新的表单字段 - Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13916694/

相关文章:

javascript - 基于 Angular.js + Node.js/PassportJS 的 SPA 上的 oauth2 身份验证

javascript - 如何添加选择选择器项目的链接?

javascript - Windows 上的 CouchDB 表示请求的资源上不存在 'Access-Control-Allow-Origin' header

javascript - 不使用angularjs向下滚动时如何验证表单?

angularjs - 如何让我的 ui-router 进入与 Controller 不同的状态?

javascript - 在 View 更改之前, Angular 值不会反射(reflect)在范围中

javascript - 在 AngularJS 中更改不同状态的动画

html - CSS Angular 的 ui-view 布局问题

Javascript 按数组中的值对哈希进行排序

angularjs - 带有 ControllerAs 绑定(bind)的 ui-router