0

I am passing a string to my isolated directive with the @ scope configuration, hoping to be able to get that string as an object on the scope, which could then be assigned a value.

HTML

<div directive model="object.property.property"></div>

JS

directive('directive', function($parse) {
    scope: {
        model: '@' 
    },
    link: function (scope, elem, attrs) {
        var object = $parse(scope.model);
        object(scope);
        scope.object.property.property = 'newstring';
        // scope.object.property.property = 'newstring';
    }
});

I was hoping that this would allow me to get object.property.property onto the scope with a value of newstring.

It doesn't seem that I am using the $parse service correctly, but most of the examples I am finding are related to using $parse to convert JSON. How can I use parse to turn this string into an object on the directive's scope?

Thanks!

4
  • are you trying to change the value of model in the directive ? Commented Jun 1, 2015 at 22:04
  • Yes! Once the model is on the scope you could do all kinds of things to it in the linking function. :-) Commented Jun 1, 2015 at 22:07
  • the entire purpose of the @ is to do text binding, not object binding. why can't you use the = in the scope? Commented Jun 1, 2015 at 23:01
  • I'm trying to evaluate some items on the parent scope and then add some properties in the attribute, like <div directive model="{{objectFromParent}}.propertyDeclaredHere"> Commented Jun 1, 2015 at 23:06

1 Answer 1

1

Is this what you are looking for?

app.directive("mainDir", function($parse) {
  return {
     restrict: 'AE',
     scope: {
       model: '@'
     }
    template: '<div> hello: {{model}} </div>',
    link: function(scope, el, attrs) {
       el.on('click', function(evt) {
           evt.stopPropagation();
           evt.preventDefault();
           var object = $parse(attrs.model);
           scope.$apply(function() {
               object(scope);
           });
           object = 'new string';
           scope.model = object;
       })
  }
 }
})

Usage:

<div main-dir model="object.property.property"></div>
Sign up to request clarification or add additional context in comments.

6 Comments

Hm thanks for reaching out! Not exactly. I simply want to make the string object.property.property available on the scope, so that it could be used in the scope to have values etc assigned to it. So if you had <div directive model="object.property.property"></div> you could get scope.object.property.property in the scope or if you had <div directive model="joke"></div> you could get scope.joke available in the scope to assign values to it. :-)
So you want to dynamically add more properties to the isolated scope object? hmmm, not sure if it is possible tho
Would adding them through html attributes be considered 'dynamic'? Hmm. $parsing doesn't allow properties to be added to the scope object?
Let say you have a template like this: <div your-directive model="model" other-model="other model" other-model-again="model again"></div> Then in your directive, you will have isolated scope like this: scope : {model : '@', otherModel:'@', otherModelAgain:'@'}
I don't think you can somehow do like this: scope.push(myOtherModel) then you go to html template and use: <div my-directive myOtherModel="my other model"> </div>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.