为什么我不能使用类似的东西?JSON使用AngularJS进行即时转换
<input type="text" ng-model="testVal">
<pre>{{JSON.stringify(testVal,null,4)}}</pre>
如果我删除了JSON.stringify()方法,我的代码工作得很好。
为什么我不能使用类似的东西?JSON使用AngularJS进行即时转换
<input type="text" ng-model="testVal">
<pre>{{JSON.stringify(testVal,null,4)}}</pre>
如果我删除了JSON.stringify()方法,我的代码工作得很好。
你可以只需使用| json
:
{{ testVal | json }}
或添加JSON
到$scope
:
$scope.JSON = JSON
然后
<pre>{{ JSON.stringify(testVal) }}</pre>
工作
双括号语法{{something}}
通知角度在$scope
对象中查找匹配项。
因此{{name}}
与您的控制器中的$scope.name
相匹配。
你可以在这里使用方法(我会建议它保持你的视图文件清洁),这将解决你的问题。
所以在控制器:
$scope.stringify = function(value, replacer, space)
{
return JSON.stringify(value,replacer,space);
}
...然后在您的视图:
<pre>{{stringify(testVal,null,4)}}</pre>
感谢您的解释! – Serban
你可以只分配功能的控制器:
$scope.stringify = JSON.stringify;
非常简单。 –
谢谢,这是最简单的方法! – Serban