![](/style/images/good.png)
![](/style/images/bad.png)
Calling Sub-Functions in ControllerAs AngularJS
source link: https://www.codesd.com/item/calling-sub-functions-in-controlleras-angularjs.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Calling Sub-Functions in ControllerAs AngularJS
Hey I am new to angularjs Im using controlleras style in angularjs as the code is presentable and net. My problem is calling subfunction in controller my code as follow
//AngularJS CODE
(function(){
'use strict';
angular.module('mAPP', ['ngMaterial']);
function helpM(){
var vm = this;
vm.SaveM = function(){
alert('Save Me Now');
}
}
function SaveCTRL(){
var vm = this;
vm.nineOne = helpM.SaveM;
}
angular.module('mAPP')
.controller('SaveCTRL', [SaveCTRL]);
})();
//HTML CODE
<div ng-controller="SaveCTRL as main" layout="column" ng-cloak="" class="md-inline-form" ng-app="mAPP">
<md-button class="md-raised md-primary" ng-click="main.nineOne()">Submit</md-button>
</div>
But the alert doesnt execute thanks a lot in advance :(
you have to make an instance of helpM
otherwise this
will be undefined
(function() {
'use strict';
angular.module('myApp', []);
function helpM() {
var vm = this;
vm.SaveM = function() {
alert('Save Me Now');
}
return vm;
}
function SaveCTRL() {
var vm = this;
vm.nineOne = new helpM().SaveM;
return vm;
}
angular.module('myApp')
.controller('SaveCTRL', SaveCTRL);
})();
<body ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="SaveCTRL as main" layout="column" ng-cloak="" class="md-inline-form">
<md-button class="md-raised md-primary" ng-click="main.nineOne()">Submit</md-button>
</div>
</body>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK