I currently have my service being executed in my AppCtrl function, although this approach is not ideal. The service should only be needed when the user navigates to page2. How can I trigger my service specifically when the page2 state is active? I attempted using resolve but encountered difficulties in making it work correctly.
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('home');
$stateProvider.state('home', {
url: '/home',
templateUrl: 'home.html'
})
$stateProvider.state('page2', {
url: '/page2/:topicId',
templateUrl: 'page2.html'
// Where do I call my service here?
});
});
app.controller('AppCtrl', ['$scope', 'getTopicContent', function($scope,getTopicContent){
getTopicContent.request().success(function(data){
$scope.myList = data;
});
}]);
app.factory('getTopicContent', ['$http', function($http){
var query = function() {
return $http({
url: "http://www.corsproxy.com/mydata.me/level1/list.php",
method: "GET"
})
}
return {
request : function(){
return query();
}
}
}]);