I am trying to switch ng-show value dynamically.
I have a controller with two methods, one to show, one to hide:
var HeaderCtrl = this;
HeaderCtrl.HelpButtonVisible = "false";
HeaderCtrl.HideHelpButton = function() {
HeaderCtrl.HelpButtonVisible = "false";
}
HeaderCtrl.ShowHelpButton = function() {
HeaderCtrl.HelpButtonVisible = "true";
}
This is the button that starts as hidden. It is also used to call the method that will hide itself:
<button id="help-button" ng-show="{{HelpButtonVisible}}" class="button button-stable icon-left ion-information-circled"
ui-sref="login" ng-click="HeaderCtrl.HideHelpButton()">Help</button>
This is the button with the method supposed to show:
<button id="login-button" class="col button button-positive button-small" ng-click="HeaderCtrl.ShowHelpButton()" ui-sref="main">Log in</button>
ng-show is update to true when I click the button above, still, the help button does not show up.
Why? I tried everything, $scope.$apply
, $scope.$digest
, tried calling the object without the {{ }}
, nothing works.
Edit:
Full controller:
angular.module('app.controllers', [])
.controller("HeaderCtrl", function( ) {
var HeaderCtrl = this;
HeaderCtrl.HelpButtonVisible = false;
HeaderCtrl.HideHelpButton = function() {
HeaderCtrl.HelpButtonVisible = false;
}
HeaderCtrl.ShowHelpButton = function() {
HeaderCtrl.HelpButtonVisible = true;
}
})
What you have is not idiomatic angular. It is important to stick to conventions when working with angular. Anything that is connected to the DOM should be attached to the $scope
object:
angular.module('app.controllers', [])
.controller("HeaderCtrl", function($scope) {
$scope.HelpButtonVisible = false;
$scope.HideHelpButton = function() {
$scope.HelpButtonVisible = false;
}
$scope.ShowHelpButton = function() {
$scope.HelpButtonVisible = true;
}
});
View:
<button id="help-button"
ng-show="HelpButtonVisible"
class="button button-stable icon-left ion-information-circled"
ui-sref="login"
ng-click="HideHelpButton()">Help</button>
You need to be using ng-model, also you cant bind to primitives like that.
controller:
this.HelperObj = {"ArrofHelperObjs":[/*YOUR OBJECTS HERE*/]};
this.ToggleHelpButton = function(obj){
var package = this.HelperObj.ArrofHelperObjs.length;
var handl = this.HelperObj.ArrofHelperObjs;
if(package == 0){
handl.push(obj);
return 200;
}
for(var x=0;x<package;x++){
if(handl[x] == obj){
handl.splice(x, 1);
return 200;
}
if(x == package-1){
handl.push(obj);
return 200;
}
}
console.log("Error: handl= "+handl);
console.log("Error: obj= "+obj);
return "Error: Conditions were supposed to have been met";
};
then html
<div ng-repeat="x in myctrl.HelperObj.ArrofHelperObjs" >
<button ng-model="x.selected" ng-click="myctrl.ToggleHelpButton(x.html); x.selected = !x.selected" ng-show="x.selected">
{{x.html}}
</label>
</div>
Two thing to point out. ng-model is how you bind to your data. When using ng-model, DONT BIND TO PRIMITIVES. It took me a while to get that in my head. In the example shared, my objects have the properties selected and html. ie.
this. package = {"selected":"false","html":"tagone"};
This would work in some cases, but most of the time you are going to have more than one object. Looks like this.
this. package = {"ArrayofObjects":[]};
inside those brackets[] goes all your objects. Like this
this. package = {"ArrayofObjects":[{"selected":"false","html":"tagone"},{"selected":"false","html":"tagtwo"}, {"selected":"false","html":"thirdtag"} ]};
Once you are set up like this, make a function that alters any of those values. That is what my ToggleHelpButton method is doing. I am changing the value of the selected property, of that element. Then in your html, use
<toggledelement ng-model="ctrl.package.ArrayofObjects[x].someproperty">
x is the index of the array of objects.
someproperty is literal selected.
you now have an element that is bound to a truthy or falsy value. Have Fun!
Try applying this to $scope
var app = angular.module('ido.controllers', []);
app.controller("HeaderCtrl", function($scope) {
angular.extend($scope, {
HelpButtonVisible: false,
HideHelpButton: function() {
$scope.HelpButtonVisible = false;
},
ShowHelpButton: function() {
$scope.HelpButtonVisible = true;
}
});
})
<button id="help-button"
ng-show="HelpButtonVisible"
class="button button-stable icon-left ion-information-circled"
ui-sref="login"
ng-click="HideHelpButton()">Help</button>
Always attach events and values to $scope. You only have access to the $scope.
When you use the scope, there is never any need to use .$digest() or .$apply(). Only when you break out of the $scope.