0

I created a directive which uses parent scope. The directive should accept a attribute i.e

<my-nice-new-directive data-hide-icon="true" />

but I do not want to isolate scope. Is it possible to just add the attribute to the $scope?

2
  • Why don't you want an isolate scope? Commented Mar 26, 2015 at 3:18
  • its a more complex directive, i just simplified it for the question in here. But yes, idealy i should refactor it. But you know how this works in companies. No budget for refactoring :-( Commented Mar 26, 2015 at 9:51

1 Answer 1

1

Consider having fun with the $parse service.

.directive('myNiceNewDirective', function () {
    return {
        restrict: 'AE',
        controller: function ($scope, $attrs, $parse) {
            var hideIcon = $parse($attrs.hideIcon)($scope);
        }
    };
})

or you could just evaluate the variable data-hide-icon="{{isIconHidden}}", in which case you may want to watch it.

.directive('myNiceNewDirective', function () {
    return {
        restrict: 'AE',
        scope: true, //this is not necessary but could be useful
        controller: function ($scope, $attrs) {
            $scope.$watch(function () {return $attrs.hideIcon;}, function (newValue, oldValue) {
               //react to change...
            });
        }
    };
})
Sign up to request clarification or add additional context in comments.

Comments

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.