javascript - Difference between ng-model and angular expression - {{}} - Stack Overflow

{{}} is working fine but ng-model is not, at the same place.I am using the following html-<body ng-a

{{}} is working fine but ng-model is not, at the same place.

I am using the following html-

<body ng-app="crud">
  Gray input fields will not be visible.
  <div ng-controller="ctrl">
    <input type="text" value="sdf" ng-model="asdf"/>
    <h1 ng-model="asdf"></h1>   <!-- this doesn't work-->
    <h1>{{asdf}}</h1>           <!-- this work-->
    </div>
  </div>
</body>

asdf is defined in this js app like this

 var app = angular.module("crud", []);
 app.controller("ctrl", ['$scope', function($scope) {
     $scope.asdf="ankur";
 }]);

Can someone explain why is it so ?

{{}} is working fine but ng-model is not, at the same place.

I am using the following html-

<body ng-app="crud">
  Gray input fields will not be visible.
  <div ng-controller="ctrl">
    <input type="text" value="sdf" ng-model="asdf"/>
    <h1 ng-model="asdf"></h1>   <!-- this doesn't work-->
    <h1>{{asdf}}</h1>           <!-- this work-->
    </div>
  </div>
</body>

asdf is defined in this js app like this

 var app = angular.module("crud", []);
 app.controller("ctrl", ['$scope', function($scope) {
     $scope.asdf="ankur";
 }]);

Can someone explain why is it so ?

Share Improve this question asked Aug 18, 2016 at 5:37 Ankur MarwahaAnkur Marwaha 1,9052 gold badges21 silver badges35 bronze badges 1
  • you should use ng-bind instead of ng-model for one way binding – z0mBi3 Commented Aug 18, 2016 at 5:41
Add a ment  | 

5 Answers 5

Reset to default 5

The ng-model directive is to be used on the input fields such as input, select for two way data binding and to get an input from a user.

Where as the one way data binding expression {{}} or ng-bind directive is used to output the data in the view.

var app = angular.module("crud", []);
app.controller("ctrl", ['$scope', function($scope) {
    $scope.asdf="ankur";
}]);
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="crud">
  Gray input fields will not be visible.
  <div ng-controller="ctrl">
    <input type="text" value="sdf" ng-model="asdf"/>
    <h1 ng-bind="asdf"></h1>
    <h1>{{asdf}}</h1>
  </div>
</div>
  

Use ng-bind for display purposes, instead of ng-model.

<h1 ng-bind="asdf"></h1>

You only want to use ng-model when binding to an element that will be editing the variable, such as a text field.

From documentation:

The ngModel directive binds an input, select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.

You are trying to use it on a <h1>. Use ng-bind instead.

According the doc

the ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.

so you will not use it for display an H1

For the brackets they will be dirty checked and refreshed in every $digest, even if it's not necessary. So it's slower. Plus thez may appear while your angularjs is bootstrapping

ng-model : This directive binds the values of AngularJS application data to HTML input controls.

{{}} This use For Printing purpose only. you can put expression also like {{2*2}} it prints 4

Refer This for study basic syntax https://angularjs/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745584396a4634441.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信