我目前正在学习AngularJS,很难理解ng-bind和ng-model之间的区别。

谁能告诉我它们有什么不同,什么时候应该用一个而不是另一个?


当前回答

ng-model

ng-model directive in AngularJS is one of the greatest strength to bind the variables used in application with input components. This works as two way data binding. If you want to understand better about the two way bindings, you have an input component and the value updated into that field must be reflected in other part of the application. The better solution is to bind a variable to that field and output that variable whereever you wish to display the updated value throughoput the application.

ng-bind

Ng-bind的工作原理与ng-model有很大不同。ng-bind是一种数据绑定方式,用于将html组件中的值显示为内部html。此指令不能用于与变量绑定,而只能用于与HTML元素内容绑定。事实上,这可以与ng-model一起使用,将组件绑定到HTML元素。这个指令对于用内部HTML内容更新div、span等块非常有用。

这个例子可以帮助你理解。

其他回答

我们可以使用ng-bind与<p>显示,我们可以使用ng-bind {{model}}的快捷方式,我们不能使用ng-bind与html输入控件,但我们可以使用ng-bind {{model}}与html输入控件的快捷方式。

<input type="text" ng-model="name" placeholder="Enter Something"/>
<input type="text" value="{{name}}" placeholder="Enter Something"/>
  Hello {{name}}
<p ng-bind="name"</p>

Ng-bind有单向数据绑定($scope——>视图)。它有一个快捷键{{val}} 它显示范围值$scope。Val插入到HTML中,Val是一个变量名。

Ng-model旨在放在表单元素内部,并具有双向数据绑定($scope—>视图和view—> $scope),例如<input Ng-model ="val"/>。

如果您在使用ng-bind还是ng-model之间犹豫不决,请尝试回答以下问题:


只需要显示数据吗?

是:ng-bind(单向绑定) No: ng-model(双向绑定)

您需要绑定文本内容(而不是值)吗?

是的:ng-bind No: ng-model(你不应该在需要value的地方使用ng-bind)

你的标签是HTML <input>?

是:ng-model(你不能使用ng-bind <input>标签) 没有:ng-bind

angular.module('testApp',[]).controller('testCTRL',function($scope) { $scope.testingModel = "This is ModelData.If you change textbox data it will reflected here..because model is two way binding reflected in both."; $scope.testingBind = "This is BindData.You can't change this beacause it is binded with html..In above textBox i tried to use bind, but it is not working because it is one way binding."; }); div input{ width:600px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <head>Diff b/w model and bind</head> <body data-ng-app="testApp"> <div data-ng-controller="testCTRL"> Model-Data : <input type="text" data-ng-model="testingModel"> <p>{{testingModel}}</p> <input type="text" data-ng-bind="testingBind"> <p ng-bind="testingBind"></p> </div> </body>

ng-model

ng-model directive in AngularJS is one of the greatest strength to bind the variables used in application with input components. This works as two way data binding. If you want to understand better about the two way bindings, you have an input component and the value updated into that field must be reflected in other part of the application. The better solution is to bind a variable to that field and output that variable whereever you wish to display the updated value throughoput the application.

ng-bind

Ng-bind的工作原理与ng-model有很大不同。ng-bind是一种数据绑定方式,用于将html组件中的值显示为内部html。此指令不能用于与变量绑定,而只能用于与HTML元素内容绑定。事实上,这可以与ng-model一起使用,将组件绑定到HTML元素。这个指令对于用内部HTML内容更新div、span等块非常有用。

这个例子可以帮助你理解。