From an AngularJS controller, how do I resolve another controller function defined in the module (dynamic ng-controller)
By : winter
Date : March 29 2020, 07:55 AM
it should still fix some issue Self explanatory fiddle: http://jsfiddle.net/5FG2n/6/ , You could try writing custom directive: code :
.directive("ngDynamicController",function($compile){
return {
terminal: true,
priority: 1000,
link:function(scope,element,attr){
var controllerProperty = scope[attr.ngDynamicController];
element.attr('ng-controller', controllerProperty);
element.removeAttr("ng-dynamic-controller");
$compile(element)(scope);
}
}
})
angular.module('app', [])
.value('InnerCtrl',InnerCtrlAsLocalVariable)
.controller('OuterCtrl', ['$scope','InnerCtrl',
function($scope, InnerCtrl) { //inject the value into the function
$scope.dynamicCtrl = InnerCtrl;
}
])
var InnerCtrlAsLocalVariable = ['$scope',
function($scope) {
$scope.message = 'from controller defined in module - want';
}
]
angular.module('app', [])
.value('InnerCtrl',InnerCtrlAsLocalVariable)
.controller('OuterCtrl', ['$scope','$injector',
function($scope, $injector) { //inject the $injector service.
// resolve the value dynamically
$scope.dynamicCtrl = $injector.get('InnerCtrl');
}
])
.controller('InnerCtrlFromModule', InnerCtrlAsLocalVariable)
angular.module('app', [])
.controller('OuterCtrl', ['$scope','$controller',
function(scope, $controller) {
scope.dynamicCtrl = $controller('InnerCtrlFromModule',{$scope:scope.$new()}).constructor;
}
])
.controller('InnerCtrlFromModule',['$scope', function($scope) {
$scope.message = 'from controller defined in module - want';
}])
|
Creating function inside Controller AngularJS
By : tim
Date : March 29 2020, 07:55 AM
like below fixes the issue Unless you are calling setTitle() in your view, you are never actually calling this function.
|
AngularJs, What is better when creating a controller?
By : Uriel Cs
Date : March 29 2020, 07:55 AM
will help you Performance wise, the first is technically better, although that cost is probably negligible. If you do it the second way, angular literally calls the .toString() function on your controller function, and then parses the string to determine the dependencies. So you can either declare them yourself, or angular will parse your function as a string to determine them. As a developer, I find it easier to read and write the second version. Just be aware that you have to change things to the first form if you plan on uglifying/minifying your code. Some tools will do this for you automatically (ngAnnotate, for example), so that you can write it the 2nd way, but have it deployed the first way.
|
How to call second controller from first controller only after certain function in controller one executed in AngularJs
By : Rajat Tandon
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to communicate between two different controllers in AngularJs. The goal I want to achieve is to use the value of variable in first controller once is selected by drop down by user not the default value which i set. but unfortunately second controller used the default value of variable in second controller. code :
var app = angular.module("myApp",[]);
//controllers declaration
app.controller('myCtrl1',function($scope, $rootScope){
$scope.selectedOption = "option-1";
$scope.change = function() {
$rootScope.$emit('changeName',{selectedOption:$scope.selectedOption});
}
});
app.controller('myCtrl2',function($scope, $rootScope){
$scope.selectedOption = "option-2";
$rootScope.$on('changeName', function(event,data) {
$scope.selectedOption = data.selectedOption;
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<span class="div1" ng-controller="myCtrl1">
<select ng-model="selectedOption" ng-change="change()">
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
<option value="option-3">Option 3</option>
<option value="option-4">Option 4</option>
<option value="option-5">Option 5</option>
</select>
</span>
<span class="div2" ng-controller="myCtrl2">
<select ng-model="selectedOption">
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
<option value="option-3">Option 3</option>
<option value="option-4">Option 4</option>
<option value="option-5">Option 5</option>
</select>
</span>
</body>
|
Angularjs Controller is not a function, got undefined after creating a Factory file
By : Csaba Antal Golubov
Date : March 29 2020, 07:55 AM
around this issue First I had a file called angular-controller.js where there was my app controller, and calling it in my html ng-controller it worked. Now I decide to make another file, a Factory file, to separate some functions from controller file, but now there are an error: Error: Argument 'LoginController' is not a function, got undefined at assertArg (angular.js:1099) at assertArgFn (angular.js:1109) at angular.js:4978 at angular.js:4560 at forEach (angular.js:137) at nodeLinkFn (angular.js:4545) at compositeLinkFn (angular.js:4191) at publicLinkFn (angular.js:4096) at angular.js:1660 at Object.$eval (angular.js:8218) , For factory file rest-services.js,change: code :
var app = angular.module("StaffLogin", []);
`var app = angular.module("StaffLogin");`
app.factory("restService", function($http) {
// code
});
|