如何将 json 作为字符串参数传递给指令 [英] how to pass a json as a string param to a directive

查看:47
本文介绍了如何将 json 作为字符串参数传递给指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试评估下面的 json 表单时,它给了我一个错误 -

When I try to eval the below json form it gives me an error -

eval("{form: 'form' , nameToMatch: 'password1'}")

为什么上面的表格无效?

Why is the above form not valid ?

但是下面的工作正常 -

However the below works fine -

eval("{form: 'form'}")

我试图将上面的 json 作为字符串传递,作为指令的参数输入.

I am trying to pass the above json as a string, as a param input to a directive.

下面是html -

<input type="password" name="password2" ng-model="user.confirmPassword" placeholder="Confirm Password" match="{form: 'form', nameToMatch: 'password1'}" required="required"/>

谢谢,穆尔塔扎

推荐答案

在 json 周围放置括号:

Put parens around your json:

 eval("({form: 'form' , nameToMatch: 'password1'})")

不过,这似乎不是一个尖锐的问题.不确定您要做什么:

Doesn't seem like an angular question though. Not sure what you're trying to do:

无论如何,要 json 传递给指令,有很多方法可以做到这一点.我不确定你为什么要这样做而不只是传递一个对象.

Anyhow, to pass the json to the directive there are lots of ways to do that. I'm not sure why you'd want to do that and not just pass an object though.

传递json有很多种方式...

passing json can be done a lot of ways...

  1. 来自您的属性对象:

  1. From your attributes object:

app.directive('foo', function () {
   return function(scope, element, attrs) {
       var obj = eval('(' + attrs.foo + ')');
   };
});

哪里

<div foo="{'test':'wee'}"></div>

  • 来自一个孤立的范围:

  • From an isolated scope:

    app.directive('foo', function () {
       return {
         restrict: 'E',
         scope: {
          'jsonIn' : '@'
         },
         link: function(scope, element, attrs) {
           var obj = eval('(' + scope.jsonIn + ')');
         };
       };
    });
    

    哪里

    <foo json-in="{'test':'wee'}"></foo>
    

  • 但是,如果可以,最好不惜一切代价避免使用原生的 eval.几乎在所有情况下都可以.如果您有一些数据,只需将其放在作用域参数上的对象中,然后通过隔离作用域上的双向属性或按名称传递它,然后对其执行角度 $eval.

    But it's by far better to avoid using the native eval at all costs, if you can. Which in almost all cases you can. If you have some data just put it in an object on a scoped parameter and pass it in either via a two-way property on an isolated scope, or by name and do an angular $eval on it.

    在...中传递一个对象

    您可以在隔离作用域上使用两种方式绑定:

    You could use two way binding on an isolated scope:

    app.directive('foo', function (){
      return {
         restrict: 'E',
         scope: {
            'data' : '='
         },
         link: function(scope, elem, attrs) {
            console.log(scope.data);
         }
      };
    });
    

    哪里

    <foo data="{ test: 'wee' }"></foo>
    

    这样做的真正好处是,如果您使用的是作用域属性,它将双向更新:

    The really cool thing about doing it this way, is if you're using a scoped property it will update bi-directionally:

    app.controller('MainCtrl', function($scope) {
        $scope.bar = { id: 123, name: 'Bob' };
    });
    

    哪里

    <foo data="bar"></foo>
    

    希望能帮到你.

    这篇关于如何将 json 作为字符串参数传递给指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆