【问题标题】:AngularJs json-formatter json attribute as scope variableAngularJs json-formatter json 属性作为范围变量
【发布时间】: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'ed schema
  • schema 是一个 JSON 对象,但即使我分配 JSON.stringify(schema) 结果也是一样的。在 console.log 结果似乎还可以。

标签: javascript json angularjs angularjs-directive


【解决方案1】:

json是json-formatter中的2路绑定,所以这里不需要插入标记{{}}

<div>
    <json-formatter open="1" json="schema"></json-formatter>
</div>

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 2014-09-29
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    相关资源
    最近更新 更多