AngularJS binding variable outside of scope and notify update?

Allan Jiang

I am new to AngularJS. Here's my question.

I have a page-level variable like this var isStateA = false. I have assigned this variable to my controllers variable like this:

var isStateA = false;

app.controller('AController',function($scope){
    $scope.shouldShow = isStateA;
});

app.controller('BController',function($scope){
    $scope.shouldShow = !isStateA;
});

The shouldShow properties are bind to ng-show accordingly.

The expected behavior is when I change isStateA to be true. The values in scope of the two controller should change, and as a result, they should perform show/hide logic.

But this is not happening with my code above. I am wondering if there's a way to do it? Like when the isStateA value changed, notify related properties to apply the latest value?

Thank you

Peter Ashwell

You are getting this wrong because you don't understand

  • AngularJS watchers
  • javascript pass by reference

AngularJS knows that values have changed in your application because of watchers. Watchers are assigned in many places, but in your case when you use $scope.shouldShow in the template. Changing the value of $scope.shouldShow will be picked up by angular and work, but changing isStateA will not, and that's because they are not the same variable. Here is the correct solution:

// Your code
app.service('stateAService', function() {
    this.isStateA = false;
})
app.controller('AController', function($scope, stateAService) {
    $scope.stateAService = stateAService;
})
app.controller('BController', function($scope, stateAService) {
    $scope.stateAService = stateAService;
})

// Template, controller A
<div ng-show="stateAService.isStateA">
</div>

// Template, controller B
<div ng-show="!stateAService.isStateA"></div>

// Some code, anywhere else in your app
app.controller('WhateverController', function($scope, stateAService) {
    this.hideBAndShowA = function() {
        stateAService.isStateA = true;
        // The change to stateAService gets picked up by the watchers, all good
    }
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS binding variable outside of scope and notify update?

From Dev

Scope variable not binding in AngularJS

From Dev

How to update $scope outside the modal bootstrap in AngularJS?

From Java

Update parent scope variable in AngularJS

From Dev

angularjs scope variable not updated outside directive

From Dev

angularjs scope variable not updated outside directive

From Dev

angular watch - update variable outside function scope

From Java

AngularJS directive does not update on scope variable changes

From Dev

AngularJS how to dynamically update part of scope with variable

From Dev

Angularjs Update Scope variable within setTimeout not working

From Dev

How to update $scope variable value in AngularJs

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

$scope variable value not accessible outside of $http success callback in AngularJS

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

Update angularJs directive scope variable on change parent scope

From Dev

angularjs binding in scope not in ui

From Dev

AngularJS var to scope binding

From Dev

angular scope variable not binding

From Dev

Notify AngularJS Controller about events from outside

From Dev

Service functions outside Angularjs scope

From Dev

Service functions outside Angularjs scope

From Dev

AngularJS $scope not accessible outside the function

From Dev

Angularjs update scope variable without re-repeating

From Dev

Angularjs update scope variable without re-repeating

From Dev

AngularJS directive scope variable doesnt update after reopening the window

From Dev

Binding directive scope with controller in AngularJS

From Dev

AngularJS Isolated Scope and Controller Binding

From Dev

Binding Angular element to scope outside of a controller

From Dev

bash: variable outside function scope

Related Related

  1. 1

    AngularJS binding variable outside of scope and notify update?

  2. 2

    Scope variable not binding in AngularJS

  3. 3

    How to update $scope outside the modal bootstrap in AngularJS?

  4. 4

    Update parent scope variable in AngularJS

  5. 5

    angularjs scope variable not updated outside directive

  6. 6

    angularjs scope variable not updated outside directive

  7. 7

    angular watch - update variable outside function scope

  8. 8

    AngularJS directive does not update on scope variable changes

  9. 9

    AngularJS how to dynamically update part of scope with variable

  10. 10

    Angularjs Update Scope variable within setTimeout not working

  11. 11

    How to update $scope variable value in AngularJs

  12. 12

    AngularJS: variable $scope inside a function appears undefined outside the function

  13. 13

    $scope variable value not accessible outside of $http success callback in AngularJS

  14. 14

    AngularJS: variable $scope inside a function appears undefined outside the function

  15. 15

    Update angularJs directive scope variable on change parent scope

  16. 16

    angularjs binding in scope not in ui

  17. 17

    AngularJS var to scope binding

  18. 18

    angular scope variable not binding

  19. 19

    Notify AngularJS Controller about events from outside

  20. 20

    Service functions outside Angularjs scope

  21. 21

    Service functions outside Angularjs scope

  22. 22

    AngularJS $scope not accessible outside the function

  23. 23

    Angularjs update scope variable without re-repeating

  24. 24

    Angularjs update scope variable without re-repeating

  25. 25

    AngularJS directive scope variable doesnt update after reopening the window

  26. 26

    Binding directive scope with controller in AngularJS

  27. 27

    AngularJS Isolated Scope and Controller Binding

  28. 28

    Binding Angular element to scope outside of a controller

  29. 29

    bash: variable outside function scope

HotTag

Archive