【发布时间】:2015-01-23 10:18:31
【问题描述】:
我正在尝试使用 json-formatter 在其中一个视图中呈现 json。
在控制器中,我正在使用通过 REST API 请求架构的服务。当请求完成时,结果被分配给范围变量。代码如下:sn-p:
.controller('SchemaCtrl', ['$scope', '$routeParams', 'Schema','$log', function($scope, $routeParams, Schema,$log){
Schema.show($routeParams.name).then(function(schema){
$scope.schema = schema;
$scope.schemaShow = true;
});
在视图中我有下一个代码:
{{schema}}
<div>
<json-formatter open="1" json="{{schema}}"></json-formatter>
</div>
不幸的是,运行示例后我收到下一个错误:
Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{schema}}] starting at [{schema}}].
{{schema}}
我知道问题是由 json-formatter 引起的。指令在角度替换 {{schema}} 之前解释为适当的值。如何使用json-formatter 通过范围变量传递值?
顺便说一句。 当我使用硬编码的 json 值时,一切正常。
【问题讨论】:
-
模式是 JSON 对象吗?还是解释的 JS 对象?即它是字符串还是 js 对象?你有
console.log'edschema -
schema 是一个 JSON 对象,但即使我分配 JSON.stringify(schema) 结果也是一样的。在 console.log 结果似乎还可以。
标签: javascript json angularjs angularjs-directive