2

I my writing universal slider directive for my app, and i need to specify that for example a control buttons in HTML code like this:

<div class="slide-wrapper" data-sample-values="{prevbtn: '.previous', nextbtn: '.next'}"></div>

How can i get this values into directive as object properties for example?

Or maybe there is another way to do resuable directive? And how i can isolate this elements from parent scope?

2 Answers 2

6
myApp.directive('slideWrapper', function() {
   return {
      restrict: 'C',
      scope: { getValues: '&sampleValues' },  // creates an isolate scope
      link:  function(scope, element, attrs) {
         var values = scope.getValues();  // evaluates expression in context of parent scope
         ...
      } 
   }
})
Sign up to request clarification or add additional context in comments.

5 Comments

From Angular docs: directive scope property: If set to {} (object hash) - then a new 'isolate' scope is created. The 'isolate' scope differs from normal scope in that it does not prototypically inherit from the parent scope. This is useful when creating reusable components, which should not accidentally read or modify data in the parent scope. docs.angularjs.org/guide/directive
How come I cannot just use = binding for the object expression? I keep getting the digest error. But this is brilliant!
@CMCDragonkai, I believe = only works with parent scope properties, not expressions.
OK but how would you use this WITHOUT creating a new isolated scope ?
This way: $parse(attrs.customAttr)(scope);
0

Do something like this:
scope.$watch(function () { return scope.$eval(attrs.sampleValues); }, function (newValue) {...});

2 Comments

Sorry, it's unclear how this helps, especially with the already selected answer. Can you elaborate on what this does or what you are trying to achieve?
In this way you don't need to create new isolated scope to get values from attribute.

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.