2016-08-18 88 views
5

{{}}正常,但ng-model不在同一位置。ng模型和角度表達式之間的區別 - {{}}

我使用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在這個定義JS應用這樣

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

有人能解釋爲什麼會這樣呢?

+0

你應該使用'NG-bind',而不是'NG-model'單程結合 – z0mBi3

回答

5

ng-model指令用於輸入字段,如輸入,選擇雙向數據綁定以及從用戶獲取輸入。

作爲單向數據綁定表達式{{}}或ng-bind指令用於輸出視圖中的數據。

var app = angular.module("crud", []); 
 
app.controller("ctrl", ['$scope', function($scope) { 
 
    $scope.asdf="ankur"; 
 
}]);
<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> 
 

0

使用ng-bind用於顯示目的,而不是ng-model

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

你只需要使用ng-model結合到將要編輯變量,如文本字段元素時。

0

documentation

ngModel的指令結合的inputselecttextarea(或定製表單控件),以使用NgModelController範圍,其中創建和由該指令露出的屬性。

您正在嘗試在<h1>上使用它。改爲使用ng-bind

0

據商務部

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. 

所以你不會使用它顯示一個H1

對於括號,他們將髒檢查,並刷新每個$消化,即使是沒有必要的。所以速度較慢。當您的angularjs引導時,可能會出現thez。

0

ng-model:該指令將AngularJS應用程序數據的值綁定到HTML輸入控件。

{{}}此用途僅用於打印目的。你可以把表情也像{{2 * 2}}它打印4

請參閱此學習基本語法https://angularjs.org/

相關問題