2015-07-10 61 views
0

如何在我的視圖中使用使用ng-bind的單向綁定?我越來越Error: [$parse:syntax] Syntax Error有以下幾點:使用該角度單向綁定語法錯誤

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

你使用什麼版本的角?我認爲你的意思是「一次性約束」不是單向的。 –

+0

我認爲這是'{{'而不是'(' – ODelibalta

+0

@ODelibalta'{{'notation with binding with the ngBind directives'')。實際上,angular會處理HTML中的{{}}以生成一個新的'' –

回答

2

表達式必須::開始被認爲是一次綁定(docs)。否則,當Angular試圖編譯它時,它會導致無效的JavaScript。

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); 
 
});
<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>

-1

嘗試:

<div>Some text here {{::oneWayBinding}} and some more text</div> 
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

這個工程,注意你不需要外部的括號,'::'不是一個JavaScript構造,它只是Angular檢查它的行爲過程的一部分,這也是爲什麼它必須在一開始。 – Agop