Hello i am trying to create an angular app. So here is one function which i am using very often in most of my controllers. Can i make one function or one method so can i can use this function everywhere just like we do in PHP.
Here is the function i have:
var responsePromise = $http.get("http://www.somewebsite.com/api/get_category_index/");
responsePromise.success(function(data, status, headers, config) {
//console.log(data);
$scope.PostCategories = data.categories;
$scope.spinner = false;
});
responsePromise.error(function(data, status, headers, config) {
alert("ajax didnt work");
});
I have multiple controller for example i am attaching 2 controllers here:
var module = angular.module('app', ['onsen', 'ngSanitize']);
module.controller('directoryControl', function($scope, $http, $rootScope) {
ons.ready(function() {
$scope.spinner = true;
var responsePromise = $http.get("http://www.somewebsite.com/api/get_category_index/");
responsePromise.success(function(data, status, headers, config) {
//console.log(data.categories);
$scope.PostCategories = data.categories;
$scope.spinner = false;
});
responsePromise.error(function(data, status, headers, config) {
alert("ajax didnt work");
});
$scope.setCurrentCategory = function(categoryName){
$scope.CurrentCategory = categoryName;
$rootScope.CurrentCategory=$scope.CurrentCategory;
}
});
});
module.controller('directoryCategoryListing', function($scope, $http, $rootScope) {
ons.ready(function() {
$scope.CurrentCategory = $rootScope.CurrentCategory;
$scope.spinner = true;
var CategoryWiseListingPromise = $http.get("http://www.somewebsite.com/api/get_category_posts/?slug="+$rootScope.CurrentCategory+"&count=50");
CategoryWiseListingPromise.success(function(data, status, headers, config) {
//console.log(data.posts);
$scope.PostDetails = data.posts;
$scope.spinner = false;
});
CategoryWiseListingPromise.error(function(data, status, headers, config) {
alert("ajax didnt work");
});
$scope.setCurrentListing = function(listingName){
$scope.CurrentListing = listingName;
$rootScope.CurrentListing=$scope.CurrentListing;
}
});
});
So i just want to make that as a common function which i can call from controller.
And same i had an issue with the URL. I want to define a URL variable which i can access from everywhere in all the controllers. Like:
var WebsiteURL = "http://www.somewebsite.com/api/"
Any one know about i can attain this!