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');
}
};
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920729a4601117.html
评论列表(0条)