I'm trying to figure out how to set a variable and use it globally. By globally i mean just outside the ng-controller scope.
The behavior is very simple. When the "gotologin" div is clicked, the login form must show up. same thing goes for register section.
But it seems the variables I used in ng-show directive are different from the one I've defined using .value and the controller. They all must be the same.
<div class="choose-box" ng-controller="mainController as mcontrol">
<div class="gotologin" ng-click="mcontrol.gotologin()">Login</div>
<div class="gotoregister" ng-click="mcontrol.gotoregister()">Register</div>
</div>
<div class="login-form" ng-show="loginclicked === true">
...
</div>
<div class="register-form" ng-show="registerclicked === true">
...
</div>
<script>
var app = angular.module('myapp', ['ngRoute']);
app.value("loginclicked" , false);
app.value("registerclicked" , false);
app.controller('mainController', function(loginclicked,registerclicked){
this.gotologin = function (){
loginclicked = true;
};
this.gotoregister = function (){
registerclicked = true;
};
});
</script>
You should set the values in the scope like:
$scope.loginclicked = false;
....
$scope.gotologin = function (){
$scope.loginclicked = true;
};
Edit:
Or try to broadcast an event with the value you need and in the other controller listen for that event.
You need to inject app values into your controller like a dependency.
I would do it as follows:
var app = angular.module('myapp', ['ngRoute']);
app.value('loginInfo', {
loginclicked: false,
registerclicked: false
});
app.controller('mainController', mainController);
mainController.$inject = ['$scope', loginInfo];
function mainController($scope,loginInfo){
$scope.loginInfo = loginInfo;
this.gotologin = function (){
$scope.loginInfo.loginclicked = true;
};
this.gotoregister = function (){
$scope.loginInfo.registerclicked = true;
};
}
Html will be <div ng-show="loginInfo.registerclicked"> and <div ng-show="loginInfo.loginclicked">
Related
I have an AngularJS script (to be run on the web) and a couple of specific functions are starting to become very long, long enough that I've built a spreadsheet to generate all of the different cases, which I then simply paste into the code.
(function(){
var app = angular
.module('app',[])
.controller('HostController',HostController);
HostController.$inject = ['$scope'];
function HostController($scope){
var host = this;
host.variable = "some text";
function reallyLongFunction(a,b) {
switch(a) {
case "something":
switch(b) {};
break;
case "something else":
switch(b) {};
break;
};
}
}
})();
I want to move them out of the main file so that it's not cluttered with these long, generated functions while I work on the rest of the programme.
I could simply move the functions directly into another file, but they need to access variables of the type host.variable, and so need to be in the scope of the main Angular app, I believe?
How can I move these functions into a different file while retaining their access to these variables?
You can move your method to separate file by creating an angular service as well. Inject that service in your controller and access the method like someSvcHelper.reallyLongFunction(a,b). This aproach will also make this method of yours as generic and will be available for other controllers as well.
But in this case you will have to pass the variables required by this function as arguments.
Using nested ng-controller's you can have access to the other controller scope in the $scope.
angular.module('app', [])
.controller('ctrl', function() {
var vm = this;
vm.value = 1;
})
.controller('auxCtrl', function($scope) {
var aux = this;
aux.result = function() {
return $scope.vm.value + 5;
}
});
<div ng-app="app" ng-controller="ctrl as vm">
<div ng-controller="auxCtrl as aux">
<input type="number" ng-model="vm.value" /> <br/>
{{vm.value}} <br/>
{{aux.result()}}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
Edit: What if i need more than one controller?
Well, in this case i think that nested controllers will be really cumbersome, so you can try a service that has an instance of the controllers scope.
angular.module('app', [])
.service('greeter', function() {
const self = this;
self.scope = {};
self.use = scope => self.scope = scope;
self.greet = () => 'Hello, ' + self.scope.myName;
})
.service('fareweller', function() {
const self = this;
self.scope = {};
self.use = scope => self.scope = scope;
self.farewell = () => 'Goodbye, ' + self.scope.myName;
})
.controller('ctrl', function($scope, greeter, fareweller) {
$scope.myName = 'Lorem Ipsum';
$scope.greeter = greeter;
$scope.fareweller = fareweller;
greeter.use($scope);
fareweller.use($scope);
});
<div ng-app="app" ng-controller="ctrl">
<input type="text" ng-model="myName"> <br>
{{greeter.greet()}} <br>
{{fareweller.farewell()}} <br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
The ng-controller directive has two ways of instantiating a controller. Vanilla and controller as syntax. How can a controller determine which way it was invoked and adjust its behavior accordingly?
For example:
<div ng-controller="myController" >
<p> {{message}} World </p>
</div>
<div ng-controller="myController as myVm">
<p> {{myVm.message}} World </p>
</div>
<div ng-controller="myController as otherVm">
<p> {{otherVm.message}} World </p>
</div>
How can I make this work in my controller?
angular.module("myApp").controller("myController", function($scope) {
function usesClassSyntax() {
//what do i put here?
return true/false
};
if (usesClassSyntax()) {
var vm = this;
} else {
var vm = $scope;
};
vm.message = "Hello";
});
by using controller as syntax we just create a new variable in our scope..
angular.module("myApp").controller("myController", function($scope) {
function usesClassSyntax() {
//i think this will help
if (typeof $scope.myVm != 'undefined')
return true;
else
return false
};
if (usesClassSyntax()) {
var vm = this;
} else {
var vm = $scope;
};
vm.message = "Hello";
});
I have a button which falls into Controller B and two block of HTML code which kind of falls under controller A...............and button falls into one block of HTML code
Example:
<div ng-controller="A">
<div ng-show="now">
<div>
<Button ng-controller="B"></Button>
</div>
</div>
<div ng-show="later">
</div>
</div>
On one button click I show up now block and later on button click of B controller I kind of hide now block and display later block.
How do I achieve this functionality?? I am not able to pass ng-show varibales between two different controller files......what should I use???
Hope this helps...!
angular.module('app', [])
.controller('A', function($scope) {
console.log('A');
$scope.state = {
now: true
};
$scope.showLater = function() {
$scope.state.later = true;
};
})
.controller('B', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="A" ng-app="app">
<div ng-show="state.now">
<div>
<button ng-controller="B" ng-click="showLater()">Show Later</button>
</div>
</div>
<div ng-show="state.later">LATER
</div>
<p> <pre ng-bind="state | json"></pre>
</p>
</div>
You could use a simple service that stores the state.
Example:
angular.module('mymodule').service('ActiveService', function() {
var service = {};
var status = false;
service.getStatus = function() {
return status;
}
service.toggle = function() {
status = !status;
}
return service;
})
And in your controller:
angular.module('mymodule').controller('SomeController', function(ActiveService) {
$scope.status = ActiveService.getStatus;
})
The Angularjs service is a singelton, so it will hold your values for you across different controllers, directives or pages.
Could also be used directly:
// Controller
$scope.service = ActiveService;
// Html
<div ng-show="service.getStatus()">
...
</div>
You can also achieve this by declaring the variable in $rootScope and watching it in controller A,
app.controller('A', function($scope, $rootScope) {
$rootScope.now = true;
$rootScope.later = false;
$rootScope.$watch("now", function() {
$scope.now = $rootScope.now;
$scope.later = !$rootScope.now;
})
});
In Controller B, you just change the value of now based on previous value like this on ng-click,
app.controller('B', function($scope, $rootScope) {
$scope.testBtn = function() {
$rootScope.now = !$rootScope.now;
}
});
I have implemented a button within different divs(now and later) in a plunker,
http://embed.plnkr.co/xtegii1vCqTxHO7sUNBU/preview
Hope this helps!
I have a page design in which i have attached the main controller to body:
<body ng-controller="firstController">
first
second
<input ng-model="hello" ng-disabled="xyz()">
<button id="test" ng-disabled="xyz()">test button</button>
{{hello}}
<div ng-view></div>
</body>
I load a template for default as
var mainApp = angular.module('mainApp',['ngRoute'])
.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/',{
templateUrl: 'partials/first.html',
controller: 'firstTemplate'
})
I my template i a check box as:
<div>
<input type="checkbox" ng-model="check">
</div>
Now i want the button #id to be disable the check box is unchecked, now i thought of using factory as these two are in different controllers:
mainApp.factory('clientId',function(){
var flag = true;
return flag;
});
mainApp.controller('firstController',['$scope','clientId',function($scope,clientId){
//$scope.check = true;
clientId.flag = false;
$scope.xyz = function(){
if(clientId){
return true;
}else{
return false;
}
}
}])
I am able to get the value from factory, now i want to update the value of the flag from different controller(template controller) and the value should reflect in first controller too so that the state of button can be updated.
mainApp.controller('firstTemplate',['$scope','clientId',function($scope,clientId){
}])
How can i update the value from second controller and make it reflect in first controller. If it is not possible is there and alternative to achieve this?
You can solve this using two ways.
1. using $rootScope.
2. using services.
using $rootScope:
<input ng-model="hello" ng-disabled="flagtoDisable">
<button id="test" ng-disabled="flagtoDisable">test button</button>
{{hello}}
<div ng-view></div>
mainApp.controller('firstController', ['$scope','$rootScope','clientId',function($scope,$rootScope,clientId){
//$scope.check = true;
clientId.flag = false;
$scope.xyz = function(){
if(clientId){
return true;
}else{
return false;
}
}
}])
mainApp.controller('firstTemplate','$scope','$rootScope','clientId',
function($scope,$rootScope,clientId){
$rootScope.flagtoDisable = false;
if($scope.check == true){
$rootScope.flagtoDisable = true;
}
}])
using Service:
mainApp.factory('clientId',function(){
var flag = {
status:false; };
return{
setFlag : funcion() {
flag.status = true;
}
getFlag : funcion() {
return flag;
}
});
mainApp.controller('firstController',['$scope','clientId',function($scope,clientId){
//$scope.check = true;
$scope.xyz = function(){
var flag = clientId.getFlag();
return flag.status;
}
}])
<input ng-model="hello" ng-disabled="xyz()">
<button id="test" ng-disabled="xyz()">test button</button>
{{hello}}
<div ng-view></div>
mainApp.controller('firstTemplate',['$scope','clientId',function($scope,clientId){
if($scope.check== true){
clientId.setFlag();
}
}])
This code is not tested.you can follow this approaches.
Yes, You can do it by using $rootScope
The ways are.
Initialize the $rootScope in your controller
Call the first controller scope variable from second controller
if 1st controller varaiable is $scope.name
now you just call $rootScope instead of $scope in second controller
like
$rootScope.name
When using the $scope controller syntax, it's simple to set a value on a parent controller's object. For example
<div ng-controller="ParentController">
{{myValue.a}}
<div ng-controller="ChildController">
{{myValue.a}}
</div>
</div>
app.controller('ParentController', function($scope) {
$scope.myValue = {};
$scope.myValue.a = 1;
});
app.controller('ChildController', function($scope) {
$scope.myValue.a = 2;
});
The above outputs:
2
2
Is there a way to achieve the same functionality with the controller as syntax without referencing $scope in the child controller?
You could do it using a service, or you could do it referencing the scope.
The behavior that you are using, scope inheritance, is often referred to as an unwanted side affect. This is why isolated scopes are used with the controllerAs syntax.
In the following example you can see we achieve the same result using sharing the myValue property on the $scope along with the controllerAs syntax.
angular.module('app', [])
.controller('ParentController', ParentController)
.controller('ChildController', ChildController);
ParentController.$inject = ['$scope'];
function ParentController($scope) {
this.myValue = {};
this.myValue.a = 1;
$scope.myValue = this.myValue;
}
ChildController.$inject = ['$scope'];
function ChildController($scope) {
this.myValue = $scope.myValue;
this.myValue.a = 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller="ParentController as parent">
parent: {{parent.myValue.a}}
<div ng-controller="ChildController as child">
child: {{child.myValue.a}}
</div>
</div>
</div>
This can be accomplished without $scope using a service:
angular.module('app', [])
.controller('ParentController', ParentController)
.controller('ChildController', ChildController)
.service('valueService', ValueService);
ParentController.$inject = ['valueService'];
function ParentController(valueService) {
this.myValue = {};
this.myValue.a = 1;
valueService.setValue(this.myValue);
}
ChildController.$inject = ['valueService'];
function ChildController(valueService) {
this.myValue = valueService.getValue();
this.myValue.a = 2;
}
function ValueService() {
var storedValue;
this.getValue = function() {
return storedValue;
}
this.setValue = function(value) {
storedValue = value;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller="ParentController as parent">
parent: {{parent.myValue.a}}
<div ng-controller="ChildController as child">
child: {{child.myValue.a}}
</div>
</div>
</div>
if you don't like to use $scope you may pass outer controller downstream, see directives communication
No, it's not possible from within ChildController.
Don't think of ControllerAs as a newer style of $scope. Each has a different use.
ControllerAs does not publish values onto scope (it actually does - via the alias, but the alias should not be assumed to be known to a child Controller since the alias is defined in the View).
I use both where needed and I use the following convention:
app.controller("ParentCtrl", function($scope){
// $scope-inherited view model
var VM = $scope.VM = ($scope.VM || {});
// controller-specific view model
var vm = this;
VM.valueVisibleToChildControllers = "foo";
vm.valueVisibleOnlyToTheView = "bar";
});