1

I'm creating an AngularJS component

var notificationComponent = {
        templateUrl: "notification.html",
        controller: ['$rootScope', notificationController],
        bindings: {
            visible: "="
        }
    };

I want to use in HTML like below:

<notification visible="$rootScope.showNotification"></notification>

Basically I want to control 'visible' property from any other component as true or false.

I tried achieving this by maintaining a variable in $rootScope named showNotification. But if I modify its value from any other component like:

$rootScope.showNotification = true;

Its not changing the 'visible' property value.

As per my understanding following code does a two way binding in AngularJS component.

bindings: {
            visible: "="
        }

Can someone tell me where I am going wrong?

3
  • But keep in mind - using $root and $rootScope is not recommended way, this may cause unpredictable behavior, you should better plan your app architecture Commented Mar 15, 2018 at 8:50
  • @LuninRoman If i don't use $rootScope to track the flag showNotification what should be the better approach to achieve it. Commented Mar 15, 2018 at 11:04
  • You can design a service, for example NotificationService with methods show and hide, or at least using Angularjs $broadcast service to emit and watch events. Commented Mar 15, 2018 at 11:49

1 Answer 1

3

It should $root in view to access $rootScope:

<notification visible="$root.showNotification"></notification>

> demo fiddle

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.