any possibility of executing controllers in or a function?
example:
$scope.send = function(){
controller1
controller2
};
My ideas is to incorporate a 'ng-click="send";' Even button on which to execute the task, greetings.
any possibility of executing controllers in or a function?
example:
$scope.send = function(){
controller1
controller2
};
My ideas is to incorporate a 'ng-click="send";' Even button on which to execute the task, greetings.
A controller, to put it in a simplified way, is a piece of code that is executed when a directive is compiled. This is why in the controllers you can access the $scope
which is a characteristic of the elements of the DOM, not the controllers in particular.
This is the configuration of ng-controller
var ngControllerDirective = [function() {
return {
restrict: 'A',
scope: true,
controller: '@',
priority: 500
};
}];
If you want two controllers to run when calling a function, you only have to include two elements that have ng-controller
and use ng-if
so that they are included in the DOM
angular.module('app', [])
.controller('ParentCtrl', function($scope) {
$scope.mostrarFragmentos = false;
$scope.habilitar = function() {
$scope.mostrarFragmentos = true;
};
})
.controller('Controller1', function($scope) {
$scope.nombre = 'Controller1';
alert('Controler1');
})
.controller('Controller2', function($scope) {
$scope.nombre = 'Controller2';
alert('Controler2');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ParentCtrl">
<button ng-click="habilitar()">Mostrar controllers</button>
<div ng-if="mostrarFragmentos">
<div ng-controller="Controller1">
{{nombre}}
</div>
<div ng-controller="Controller2">
{{nombre}}
</div>
</div>
</div>
It is important that you use ng-if
and not ng-show
since the second only hides the element while the first deletes it or adds it to the DOM, causing a new compilation of the directive and therefore a new execution of the code of the controller.
Obviously you should only do this if you are trying to include fragments of full functionality, otherwise you can move the logic you want to execute the particular method to a single controller and do it all from there
angular.module('app', [])
.controller('SimpleCtrl', function($scope) {
$scope.mostrar = false;
$scope.name1 = 'Controller1';
$scope.name2 = 'Controler2';
$scope.habilitar = function() {
$scope.mostrar = true;
alert('Codigo a ejecutar');
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="SimpleCtrl">
<button ng-click="habilitar()">Mostrar</button>
<div ng-show="mostrar">
<div>{{name1}}</div>
<div>{{name2}}</div>
</div>
</div>
ng-click
does not work like this. The controller contains functions that can be called from ng-click
. For example:
HTML:
<div ng-controller="MyCtrl">
<button ng-click="func()">Clic</button>
</div>
Javascript:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.func = function(){
alert('Hola!')
}
}
Put the task inside the function that is inside the controller.