doe
doe

Reputation: 148

Javascript outside angular js

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

Answers (1)

smnbbrv
smnbbrv

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

Related Questions