2016-11-28 106 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 }]}