使用ng-model angularjs时未显示的表单值

前端之家收集整理的这篇文章主要介绍了使用ng-model angularjs时未显示的表单值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于编辑产品的表单,该值显示在检查器中,但不在表单中.当我从代码删除ng-model时,它们会显示.

在此行中,值显示在inspect元素中,但不在以下形式中:

<input type="text" class="form-control" placeholder="Product naam" 
value="{{product.title}}" ng-model="formData.title"/>

在此行中,格式如下:

<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'

}

猜你在找的Angularjs相关文章