I'am very new to Angularjs.
I created an Angularjs Service to store some "global" vars. It looks like this:
.factory('authVars', function() {
var sdo = {
baseBackendUrl: 'https://www.myurl.com',
user: '',
password: '',
token: '',
isLogged: false
};
return sdo;
})
Now I want use ng-show/hide in different controllers.
<div class="alert" ng-hide="authVars.isLogged">
<strong>whatEver</strong>
</div>
Is this even possible? Or is it better to store this in the rootScope?
For a bit of help I would be very grateful ;-) thx
Just register your factory to every controller. This is a goal service for code reuse. Service is like utility, you write it once and can use it in many controllers
JS
myApp.factory('authVars', function() {
var sdo = {
baseBackendUrl: 'https://www.myurl.com',
user: '',
password: '',
token: '',
isLogged: false
};
return {
getSdo: function() {
return sdo;
}
};
})
function MyCtrl($scope, authVars) {
$scope.authVars = authVars.getSdo();
}
Demo Fiddle
It is 100% ok to put it in service! Just don't forget to inject it in relevant controllers:
var Ctrl = function($scope,authVars){
$scope.authVars = authVars;
}
Example: http://jsfiddle.net/cherniv/pzFrs/2/
you need to inject service in controller as below.
app.controller('ctrl',function($scope, authVars){
$scope.authVars=authVars;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With