Reputation: 148
In Angular JS instead of using the service in a module, can I create Javascript functions outside the controller and call it when I want to?
For example:
var UrlPath="http://www.w3schools.com//angular//customers.php"
//this will contain all your functions
function testing_Model ($http){
var self=this;
self.getRecords=function(){
$http({
Method:'GET',
URL: UrlPath,
}).success(function(data){
self.record=data.records;
});
}
self.getRecords();
}
angular.module("myApp", []);
app.controller('myController', function ($scope, $http) {
$scope.testing_Model = new testing_Model();}
When I run the code above, it says "$http is not a function".
Thanks in advance.
Upvotes: 1
Views: 48
Reputation: 24541
You need to pass $http to this function...
$scope.testing_Model = new testing_Model($http);
Although it will work it obviously goes against the idea of Angular. You must inject your function as a service in your controller to write testable code, track dependencies and just respect other developers who will be going to support your project later.
Upvotes: 1