admin管理员组文章数量:1405516
I'm new to angular and using a function to load data using $http, that worked for me. After that I decided to do some form validation to see it's behaviour. I can do it individually but a bit confused on how can I merge my code with the above so that both of my functions work well. The code is as:
var App = angular.module('App', []);
App.controller('DataCtrl', function($scope, $http) {
$http.get('any url or file')
.then(function(result) {
$scope.data = result.data;
});
});
The controller is defined in the body attribute like this:
<body ng-controller="DataCtrl">
Now when I put the other function in the same app.js file, it doesn't work, can anybody help me out on this, the validation code is something like this:
App.controller('DataCtrl', function($scope) {
$scope.submitForm = function(isValid) {
if (isValid) {
alert('submitting data');
}
};
});
The only thing I need to do this is to make them work like this: 1. Load data (working) 2. Implement validation with the help of second code block.
I'm new to angular and using a function to load data using $http, that worked for me. After that I decided to do some form validation to see it's behaviour. I can do it individually but a bit confused on how can I merge my code with the above so that both of my functions work well. The code is as:
var App = angular.module('App', []);
App.controller('DataCtrl', function($scope, $http) {
$http.get('any url or file')
.then(function(result) {
$scope.data = result.data;
});
});
The controller is defined in the body attribute like this:
<body ng-controller="DataCtrl">
Now when I put the other function in the same app.js file, it doesn't work, can anybody help me out on this, the validation code is something like this:
App.controller('DataCtrl', function($scope) {
$scope.submitForm = function(isValid) {
if (isValid) {
alert('submitting data');
}
};
});
The only thing I need to do this is to make them work like this: 1. Load data (working) 2. Implement validation with the help of second code block.
Share Improve this question asked Jun 20, 2015 at 4:47 samsam 4271 gold badge8 silver badges21 bronze badges 1- what do you mean by its not working? Is the function not getting invoked? – CuriousMind Commented Jun 20, 2015 at 5:01
2 Answers
Reset to default 3Just try to explain how the thing is working :)
When browser encounter this line <body ng-controller="DataCtrl">
it will look for the defination of controller that is registered with the angular js.
But in your case I guess you are registed controller two times like
FIRST
App.controller('DataCtrl', function($scope, $http) {
$http.get('any url or file')
.then(function(result) {
$scope.data = result.data;
});
});
SECOND
App.controller('DataCtrl', function($scope) {
$scope.submitForm = function(isValid) {
if (isValid) {
alert('submitting data');
}
};
});
So one of the definition overwrites the other depending on the order of the registered method present in your script file.
So to overe this you need to use only one registered controller:-
App.controller('DataCtrl', function($scope, $http) {
$http.get('any url or file').then(function(result) {
$scope.data = result.data;
});
$scope.submitForm = function(isValid) {
if (isValid) {
alert('submitting data');
}
};
});
Code credit :- S Vinesh
Just use it like below
var App = angular.module('App', []);
App.controller('DataCtrl', function($scope, $http) {
$http.get('any url or file').then(function(result) {
$scope.data = result.data;
});
$scope.submitForm = function(isValid) {
if (isValid) {
alert('submitting data');
}
};
});
本文标签: javascriptUsing Multiple Functions in AngularJsStack Overflow
版权声明:本文标题:javascript - Using Multiple Functions in AngularJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744920728a2632278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论