resources - Changing the URL/IP of angularjs ngresource in ionic -
i use user selected ip address change rest url more 1 time, may have serval server ip addresses, after factory file loaded after app start, ip addresses ($rootscope.baseurl) cannot changed.
is there anyway can change path again?
p.s. console.log("$rootscope.baseurl") can output value, after return part, not working.
controller:
$scope.authenticateuser = function(){ $rootscope.$broadcast('ipchanged', $scope.user.ip); }
factory:
.factory( 'models', function ($rootscope, $resource, constants) { $rootscope.$on('ipchanged', function(event, data) { $rootscope.baseurl = "http://" + data + "/rest" console.log("$rootscope.baseurl") }); return{ appmaster_user_session: $resource($rootscope.baseurl + '/user/session', { app_name: constants.api.appname }, { 'post': {method:'post'}, 'put': {method: 'put'} } ),
you can make service configurable using provider recipe.
myapp.provider('models', function modelsprovider() { var apiurl = 'http://www.deafault.com'; this.apiurl = function(value) { apiurl = value; }; this.$get = [function modelsfactory() { return { appmaster_user_session: $resource(apiurl + '/user/session', { app_name: constants.api.appname }, { 'post': {method:'post'}, 'put': {method: 'put'} } ) } }]; });
you need set apiurl in config section of module.
if want different url each time make resource contain function recieves apiurl.
myapp.provider('models', function modelsprovider() { this.$get = [function modelsfactory() { return { getsessionresource: function(apiurl) { return $resource(apiurl + '/user/session', { app_name: constants.api.appname }, { 'post': {method:'post'}, 'put': {method: 'put'} } ) } }]; });
Comments
Post a Comment