Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Put parens around your json:</p> <pre><code> eval("({form: 'form' , nameToMatch: 'password1'})") </code></pre> <p>Doesn't seem like an angular question though. Not sure what you're trying to do:</p> <p>Anyhow, to <em>pass</em> 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.</p> <p>passing json can be done a lot of ways... </p> <ol> <li><p>From your attributes object:</p> <pre><code>app.directive('foo', function () { return function(scope, element, attrs) { var obj = eval('(' + attrs.foo + ')'); }; }); </code></pre> <p>where</p> <pre><code>&lt;div foo="{'test':'wee'}"&gt;&lt;/div&gt; </code></pre></li> <li><p>From an isolated scope:</p> <pre><code>app.directive('foo', function () { return { restrict: 'E', scope: { 'jsonIn' : '@' }, link: function(scope, element, attrs) { var obj = eval('(' + scope.jsonIn + ')'); }; }; }); </code></pre> <p>where</p> <pre><code>&lt;foo json-in="{'test':'wee'}"&gt;&lt;/foo&gt; </code></pre></li> </ol> <p>But it's by far better to avoid using the native <code>eval</code> 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.</p> <p><strong>EDIT: The pass an object in...</strong></p> <p>You could use two way binding on an isolated scope:</p> <pre><code>app.directive('foo', function (){ return { restrict: 'E', scope: { 'data' : '=' }, link: function(scope, elem, attrs) { console.log(scope.data); } }; }); </code></pre> <p>where</p> <pre><code>&lt;foo data="{ test: 'wee' }"&gt;&lt;/foo&gt; </code></pre> <p>The really cool thing about doing it this way, is if you're using a scoped property it will update bi-directionally:</p> <pre><code>app.controller('MainCtrl', function($scope) { $scope.bar = { id: 123, name: 'Bob' }; }); </code></pre> <p>where</p> <pre><code>&lt;foo data="bar"&gt;&lt;/foo&gt; </code></pre> <p>I hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload