私は現在AngularJSを学習していますが、との違いを理解するのに苦労していng-bind
ますng-model
。
誰かがそれらがどのように異なり、一方が他方の上にいつ使用されるべきかを教えてもらえますか?
私は現在AngularJSを学習していますが、との違いを理解するのに苦労していng-bind
ますng-model
。
誰かがそれらがどのように異なり、一方が他方の上にいつ使用されるべきかを教えてもらえますか?
ng-bindには一方向のデータ バインディングがあります ($scope --> ビュー)。変数名であるhtmlに挿入され{{ val }}
たスコープ値を表示するショートカットがあります。$scope.val
val
ng-modelはフォーム要素内に配置することを意図しており、双方向のデータ バインディング ($scope --> ビューとビュー --> $scope) を備えて<input ng-model="val"/>
います。
toshの答えは、質問の核心にうまく到達します。ここにいくつかの追加情報があります....
ng-bind
ng-model
どちらも、ユーザーに出力する前にデータを変換するという概念を持っています。そのために、filter を使用し、 formattersng-bind
を使用しng-model
ます。
では、フィルタng-bind
を使用してデータを変換できます。例えば、
<div ng-bind="mystring | uppercase"></div>
、
またはもっと簡単に:
<div>{{mystring | uppercase}}</div>
独自のフィルターを作成することもできますがuppercase
、組み込みの角度フィルターであることに注意してください。
ng-model フォーマッタを作成するにはrequire: 'ngModel'
、 を行うディレクティブを作成します。これにより、そのディレクティブが ngModel の にアクセスできるようになりますcontroller
。例えば:
app.directive('myModelFormatter', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, controller) {
controller.$formatters.push(function(value) {
return value.toUpperCase();
});
}
}
}
次に、パーシャルで:
<input ngModel="mystring" my-model-formatter />
これは基本的に、上記の例でフィルターが行っているng-model
ことと同じです。uppercase
ng-bind
では、ユーザーが の値を変更できるようにする場合はどうすればよいでしょmystring
うか? モデル-->ビューng-bind
からの一方向バインディングのみがあります。ただし、ビュー-->モデルからバインドできます。これは、ユーザーがモデルのデータを変更できるようにすることを意味し、パーサーを使用してユーザーのデータを合理化された方法でフォーマットできます。これは次のようになります。ng-model
app.directive('myModelFormatter', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, controller) {
controller.$parsers.push(function(value) {
return value.toLowerCase();
});
}
}
}
ng-model
フォーマッター/パーサーの例のライブプランカーで遊んでください
ng-model
組み込みの検証もあります。$parsers
または関数を変更して、ngModelの関数$formatters
を呼び出すだけです。controller.$setValidity(validationErrorKey, isValid)
$parsers
Angular 1.3 には、 orの代わりに検証に使用できる新しい $validators 配列$formatters
があります。
このディレクティブは優先度 1 で実行されます。
ジャバスクリプト
angular.module('inputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = '1';
}]);
CSS
.my-input {
-webkit-transition:all linear 0.5s;
transition:all linear 0.5s;
background: transparent;
}
.my-input.ng-invalid {
color:white;
background: red;
}
HTML
<p id="inputDescription">
Update input to see transitions when valid/invalid.
Integer is a valid value.
</p>
<form name="testForm" ng-controller="ExampleController">
<input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input"
aria-describedby="inputDescription" />
</form>
ngModel は以下を担当します。
このディレクティブは、優先度レベル 0 で実行されます。
ジャバスクリプト
angular.module('bindExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.name = 'Whirled';
}]);
HTML
<div ng-controller="ExampleController">
<label>Enter name: <input type="text" ng-model="name"></label><br>
Hello <span ng-bind="name"></span>!
</div>
ngBind は以下を担当します。
ng-モデル
AngularJS の ng-model ディレクティブは、アプリケーションで使用される変数を入力コンポーネントにバインドする最大の強みの 1 つです。これは、双方向のデータバインディングとして機能します。双方向バインディングについてよりよく理解したい場合は、入力コンポーネントがあり、そのフィールドに更新された値をアプリケーションの他の部分に反映する必要があります。より良い解決策は、変数をそのフィールドにバインドし、アプリケーションを介して更新された値を表示したい場所にその変数を出力することです。
ng バインド
ng-bind は ng-model とは大きく異なります。ng-bind は、html コンポーネント内の値を内部 HTML として表示するために使用される 1 つの方法のデータ バインディングです。このディレクティブは、変数とのバインドには使用できませんが、HTML 要素のコンテンツとのバインドにのみ使用できます。実際、これを ng-model と一緒に使用して、コンポーネントを HTML 要素にバインドできます。このディレクティブは、内部の HTML コンテンツで div、span などのブロックを更新する場合に非常に便利です。
この例は、理解するのに役立ちます。
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>