私は次のように私のコードを持っています:
one_time.js
var sample = angular.module('sample', []);
sample.controller('OneTimeController', function () {
this.name = 'John';
this.blah = 'sdflk';
});
index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="one_time.js"></script>
</head>
<body ng-app="sample">
<div ng-controller="OneTimeController as test">
<p>Input something in the input box:</p>
<p>Name: <input type="text" ng-model="test.name" value="John"></p>
<p ng-bind="test.name"></p>
<p ng-bind="::test.name"></p>
<p ng-if="test.name && test.blah">testing</p> <!-- works perfectly-->
<p ng-if="::test.name && test.blah">testing with first expression as one time binded</p> <!-- works but the second property is not one time binded-->
<p ng-if="::test.name && ::test.blah">testing with one time bind</p><!-- doesn't work throws syntax error-->
</div>
</body>
</html>
nameプロパティを1回バインドするng-if条件で<p>
タグを表示しようとしています。 私はそれが変わらないことをすでに知っているので、ng-ifでも一度バインドしてみたかったのです。 ワンタイムバインディングを使用しない場合、またはこの場合は<p ng-if="::test.name && test.blah">testing with first expression as one time binded</p>
の最初の式でワンタイムバインディングのみを使用する場合は完全に機能しますが、複数の式で1回のバインディングを使用しようとするとガード演算子は、構文エラーとしてエラーをスローします。
Error: [$parse:syntax] Syntax Error: Token ':' not a primary expression at column 14 of the expression [test.name && ::test.blah && ::test.blah] starting at [::test.blah && ::test.blah].
http://errors.angularjs.org/1.3.14/$parse/syntax?
これはバグですか、それとも本物の構文エラーですか? 私はangularjsバージョン1.3.14を使用しています。
式を使用したオンタイムバインディングには()
を使用する必要があると思います。例: <p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p>
@piernikThanksは魅力のように機能します。 (::test.name) && (::test.blah)
を試していましたが、最初の::
1回限りのバインディングをアクティブにしているようです。 :+1:
ワンタイムバインディング(::)は、ng-if内で使用されるすべての式に適用されます。
<p ng-if="::test.name && test.blah"> is same as <p ng-if="::(test.name && test.blah)">
残念ながら、Angularjsの単一の式で1回限りのバインディングと一方向のバインディング式を混在させることはできません。
最も参考になるコメント
式を使用したオンタイムバインディングには
()
を使用する必要があると思います。例:<p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p>