javascript - Using Multiple Functions in AngularJs -
i'm new angular , using function load data using $http, worked me. after decided form validation see it's behaviour. can individually bit confused on how can merge code above both of functions work well. code 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 defined in body attribute this:
<body ng-controller="datactrl">
now when put other function in same app.js file, doesn't work, can me out on this, validation code this:
app.controller('datactrl', function($scope) { $scope.submitform = function(isvalid) { if (isvalid) { alert('submitting data'); } }; });
the thing need make them work this: 1. load data (working) 2. implement validation of second code block.
just try explain how thing working :)
when browser encounter line <body ng-controller="datactrl">
defination of controller registered angular js. in case guess registed controller 2 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 1 of definition overwrites other depending on order of registered method present in script file.
so overcome need use 1 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
Comments
Post a Comment