Search code examples
angularjsangularjs-scope

In angular ng-show without ng-click?


So, I'd like to show a div when a flag is set to true. I've been able to achieve this with ng-show by toggling the value of the flag with a button click.

However, I'd like to use ng-show when that flag is changed by something other than a button click. There is a stream of events that I'm taking in from my backend service, and one of the events would flip this flag, but ng-show never seems to fire off unless I trigger it specifically with a button. Right now, the only way that I can get that effect that I want is just by directly calling **document.getElementById().hidden = true/false.**

Any ideas? Thank you!


Solution

  • Maybe I'm misinterpreting your use case but it seems very simple: just use a variable in your controller / component and reference that variable in your ng-show.

    // in your html
    ng-show="myFlag"
    
    // in your js
    $scope.myFlag = true;
    

    It may be a little more complicated depending on how you are using that backend service.