我得到了一个用于编辑产品的表单,该值在检查器中显示,但是不在表单中显示。当我从代码中删除ng-model时,它们会显示出来。
在此行中,值显示在inspect元素中,但不以以下形式显示:
<input type="text" class="form-control" placeholder="Product naam"
value="{{product.title}}" ng-model="formData.title"/>
在此行中,该值在inspect元素中以以下形式显示:
<input type="text" class="form-control" placeholder="Product naam"
value="{{product.title}}"/>
但是我需要ng-model来传递数据。
有什么建议么?
您不需要输入标签中的value属性。ng-model已经为您完成了绑定。如果您在控制器中定义formData.title,它将反映您输入的值。
同样,如果用户更改输入的值,它将反映在控制器中声明的值上。
这是著名的AngularJS双向数据绑定。框架的一项令人敬畏且危险的功能
看看这个小提琴:
http://jsfiddle.net/relferreira/kLcqwuqf/
HTML:
<div ng-app="app">
<div ng-controller="MainController">
<input type="text" data-ng-model="test">
</div>
</div>
JS:
angular.module('app', []);
angular.module('app')
.controller('MainController', mainController)
mainController.$inject = ['$scope'];
function mainController($scope){
$scope.test = 'value of the input'
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句