Reputation: 1725
I have an existing application that Im moving to Angular and I have some questions. My new Angular app has four routes:
In my old app I had AJAX calls to PHP that would query MySQL and return the information as needed, but I could tie everything together using jQuery and call my AJAX calls from one centrally located .js file.
Now, I need to write a call to $HTTP to do the same thing as my AJAX calls did, but I dont know where to put everything!
For example I have the following sample $HTTP function I found online:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("users.php")
.then(function(response) {
$scope.users = response.data;
});
});
</script>
Where does this go in the Angular file structure? I intend to call it from the Activity route, which creates another question. Since the route pages are based on templates how do I call the function from the route or template so I can see the results on the Activity page?
Upvotes: 1
Views: 65
Reputation: 560
Factory/Service is what you are looking for. Ajax call should be made inside a factory method which in turn returns the data to the controller (myCtrl in your example).
You should bind your controller (myCtrl) with the view (Activity) in the route provider itself.
Here is an example that will give you a clear picture.
Upvotes: 3