2016-11-28 109 views
0

我只是試圖讓AngularJS表達式顯示在屏幕上。但是,大括號之間沒有任何顯示。我用ng-inspector檢查了應用程序,雖然我看到一個正在用ng-model指令創建的對象,但我無法用對象鍵顯示值。此外,出於測試目的,我甚至無法獲得簡單的數學表達式來顯示。AngularJS表達式不顯示

以下是我正在處理的內容。

<body ng-app="angularApp"> 
    <div ng-controller="firstCtrl"> 
     <input ng-model="project.completed" type="checkbox"> 
     <input ng-model="project.title" type="text" placeholder="Project Title"> 
     <label> 
      {{project.title}} 
      1+2={{1 + 2}} 
     </label> 
     <input ng-model="project.time" type="text" placeholder="Project Time"> 
     <label for="">{{project.time}}</label> 
     <button ng-click="helloWorld()">Press Me</button> 
    </div> 
</body> 

...這是控制器:

angular.module('angularApp', []) 

.controller('firstCtrl', function($scope) { 
    $scope.helloWorld = function() { 
     console.log('You just pressed the button.'); 
    }; 

    $scope.project = { 
     completed :false, 
     title :'test', 
    }; 

}); 

,在標籤顯示了唯一的一句話是 '1 + 2 ='。

更新:花費了大量的時間試圖調試這個後,我已經能夠獲得數學表達式的第一個值顯示 - '1'。我通過在'+'運算符周圍添加空格來實現此目的。儘管如此,完整的表達並沒有評估。

+0

張貼您的控制器 – Sajeetharan

+0

任何控制檯錯誤? –

+2

你有什麼地方'ng-app =「angularApp」'? – devqon

回答

0

如果您使用其他模板引擎(如Twig,Liquid或Django),則可能會剝離大括號。這會導致值不能正確顯示或評估。

我找到的解決方案是編輯插入字符或$ interpolateProvider像這樣的控制器內部:

angular.module('angularApp', []).config(function($interpolateProvider){ 
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}'); 
}) 

然後,只是包裝的新符號的表達,如:

{[{ 1+2 }]} 

。 ..或

{[{ project.title }]}