8

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

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

为什么上述表格无效?

但是下面的工作正常 -

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

我正在尝试将上述 json 作为字符串传递,作为指令的参数输入。

下面是html -

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

谢谢,穆尔塔萨

4

5 回答 5

23

在你的 json 周围加上括号:

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

不过,这似乎不是一个有角度的问题。不确定您要做什么:

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

传递 json 可以有很多方法......

  1. 从您的属性对象:

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

    在哪里

    <div foo="{'test':'wee'}"></div>
    
  2. 从一个孤立的范围:

    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。

编辑:传递一个对象...

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

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

在哪里

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

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

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

在哪里

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

我希望这会有所帮助。

于 2013-02-04T14:06:39.947 回答
2

See also. @Blesh has mentioned in passing, but not emphasized: Angular provides you with a 'safe'-ish version of eval(), which works perfectly for passing declarative objects/arrays into your directive if you don't want to declare them on your scope, and wisely don't want to use native eval(). In the OP's example, just use this inside the directive:

angular.$eval(attrs.match);
于 2014-04-05T16:06:53.763 回答
2

您似乎正在尝试在表单中确认密码。有很多方法可以做到这一点,而无需借助 JSON 在 AngularJS 中传递值。我在网上找到的最有用的资源来自这个 Google Group 线程:

1) http://jsfiddle.net/pkozlowski_opensource/GcxuT/23/将第二个字段中的值与第一个字段的模型值进行比较

2) http://jsfiddle.net/S8TYF/将第二个字段中的 值与第一个字段的输入值进行比较

差异可能很细微,但会产生实际影响:使用 (2),只要您开始在第一个字段中输入任何内容,确认验证就会启动。使用 (1) 确认验证仅在第一个字段有效后才会启动。在电子邮件确认示例中,这意味着在电子邮件验证错误被整理出来之前,您不会开始显示确认错误(因此用户可以同时专注于一个错误)。

来源:https ://groups.google.com/d/msg/angular/R4QeNsNksdY/migbplv8GxIJ

从第一个链接开始,该指令的使用如下:

<label>e-mail</label>
<input name="email" type="email" required ng-model="email">  

<label>repeat e-mail</label>
<input name="emailRepeat" type="email" required 
       ng-model="emailRepeat" 
       ui-validate-equals="email">

其中ui-validate-equals指令指向email在第一个输入中定义的模型。

如果您也想在那里寻找解决问题的其他想法,有一些StackOverflow 的答案。

于 2013-02-04T16:23:21.313 回答
1

不过,这似乎不是一个有角度的问题。

同意 - vanilla JS 可以很好地处理这个问题。

// Markup (HTML)
<div mydirective='{"test1": "foo", "test2": "bar"}'></div>

// App (JS)
JSON.parse(attrs['mydirective']);
于 2014-07-16T22:48:18.000 回答
0

以角度使用 JSON.parse(string)。确保您的参数是字符串格式。

于 2014-10-07T16:03:16.653 回答