角度单向绑定语法错误

时间:2015-07-10 18:12:43

标签: angularjs

如何在视图中使用ng-bind使用单向绑定?我收到Error: [$parse:syntax] Syntax Error以下内容:

ng-bind="'Some text here' + (::oneWayBinding) + ' and some more text'"

3 个答案:

答案 0 :(得分:3)

我自己刚刚完成了解决这个问题的工作。我能够使用以下方法解决它。

ng-bind="::('Some text here' + (oneWayBinding) + ' and some more text')"

::语法需要超出绑定到元素的所有内容,而不仅仅是角度变量。最终我会建议把它分解成类似

的东西

<p>Some text here <span ng-bind="::oneWayBinding"></span> and some more text</p>

后者是单向绑定的可靠解决方案。希望这有帮助!

答案 1 :(得分:2)

表达式必须开始::才能被视为一次性绑定(docs)。否则,当Angular尝试编译它时,它只会导致无效的JavaScript。

&#13;
&#13;
var app = angular.module('app', []);

app.controller('Ctrl', function($scope, $timeout) {
  $scope.oneTimeBinding = 'FOO';
  
  // change oneTimeBinding property after 2 seconds
  $timeout(function() { $scope.oneTimeBinding = 'NOT FOO'; }, 2000);
});
&#13;
<div ng-app="app" ng-controller="Ctrl">
  <div ng-bind="::'One time binding: ' + oneTimeBinding"></div>
  <div ng-bind="'Not one time binding: ' + oneTimeBinding"></div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

尝试使用:

<div>Some text here {{::oneWayBinding}} and some more text</div>
相关问题