ng模型和角度表达之间的差异 - {{}}

Ank*_*aha 5 html javascript angularjs

{{}}工作正常,但ng-model不在同一个地方.

我使用以下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>
Run Code Online (Sandbox Code Playgroud)

asdf是在这个js应用程序中定义的

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

有人可以解释为什么会这样吗?

Abd*_*med 5

ng-model指令用于输入字段,例如输入,选择双向数据绑定以及从用户获取输入.

单向数据绑定表达式{{}}或ng-bind指令用于在视图中输出数据的位置.

var app = angular.module("crud", []);
app.controller("ctrl", ['$scope', function($scope) {
    $scope.asdf="ankur";
}]);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/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>
  
Run Code Online (Sandbox Code Playgroud)