config
(function() {
angular.module('plunker', ['ngRoute']);
angular.module('plunker').config(['$routeProvider', moduleConfig]);
function moduleConfig($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'home.html', //url of the template
controller: 'HomeCtrl', //name of the controller
controllerAs: 'hCtrl' //how it should be referred in the template
})
.when('/profile', {
templateUrl: 'profile.html', //url of the template
controller: 'ProfileCtrl', //name of the controller
controllerAs: 'pCtrl' //how it should be referred in the template
})
.otherwise({
redirectTo: '/home'
});
}
//controllers should be in their own .js files
angular.module('plunker').controller('HomeCtrl', HomeCtrl);
angular.module('plunker').controller('ProfileCtrl',['myservice', ProfileCtrl]);
function HomeCtrl() {
var hCtrl = this;
alert('HomeCtrl');
}
function ProfileCtrl(myservice) {
var pCtrl = this;
myservice.retrive(function(data){
alert('profile');
});
}
})();
myservice
(function(){
angular.module('plunker')
.factory('myservice',myservice);
myservice.$inject=['$http'];
function myservice($http) {
var factory={
retrive:retrive
};
return factory;
function retrive(callback){
var url = 'test.json';
var params = {
callback: 'angular.callbacks._0'
};
$http.jsonp(url, {params: params}).then(callback);
}
}
})();
I wrote this controller and service for a particular page, let say "/profile". profile controller uses a service which has JSONP request. when I go to that page("/profile") everything works and JSONP callback executes. But when I go to "/index" after "/profile" and come back again to "/profile" JSONP call fires but callback in controller is not executed. can some one spot the error
plnkr with code http://plnkr.co/edit/fswywkpsH6xl5XU0YplK?p=preview
In this plnkr the callback in profile controller executes only once