لدي الكود الخاص بي على النحو التالي:
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>
أحاول إظهار علامة <p>
مع شرط ng-if حيث أريد ربط خاصية الاسم مرة واحدة. أعلم بالفعل أنه لن يتغير ، لذا أردت تجربة الربط مرة واحدة في ng-if أيضًا. إنه يعمل بشكل مثالي عندما لا أستخدم الربط لمرة واحدة أو عندما أستخدم الربط مرة واحدة فقط مع التعبير الأول في هذه الحالة <p ng-if="::test.name && test.blah">testing with first expression as one time binded</p>
، ولكن عندما أحاول استخدام الربط مرة واحدة في أكثر من تعبير واحد إلى جانب عامل حارس يرمي لي خطأ كخطأ في بناء الجملة:
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?
هل هذا خطأ أو خطأ نحوي حقيقي؟ أنا أستخدم الإصدار 1.3.14 من angularjs.
أعتقد أنه يجب عليك استخدام ()
للربط في الوقت مع التعبيرات على سبيل المثال: <p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p>
piernik شكرا يعمل مثل السحر. كنت أحاول (::test.name) && (::test.blah)
ولكن يبدو أن أول ::
ينشط الربط لمرة واحدة. : +1:
ينطبق الربط لمرة واحدة (: :) على جميع التعبيرات المستخدمة داخل ng-if.
<p ng-if="::test.name && test.blah"> is same as <p ng-if="::(test.name && test.blah)">
لسوء الحظ ، لا يمكننا مزج الربط مرة واحدة مع تعبيرات ربط أحادية الاتجاه في تعبير واحد في الزاوية js.
التعليق الأكثر فائدة
أعتقد أنه يجب عليك استخدام
()
للربط في الوقت مع التعبيرات على سبيل المثال:<p ng-if="::(test.name && test.blah)">testing with first expression as one time binded</p>