I also do not like to use a function to handle a property that resolves over and over in every controller and service. Seem I'm not alone: ββD
I did not try to get the result with the promise as a variable, of course, nothing. But I found and use the solution below to access the result as a property.
First, write the result in the property of your service:
app.factory('your_factory',function(){ var theParentIdResult = null; var factoryReturn = { theParentId: theParentIdResult, addSiteParentId : addSiteParentId }; return factoryReturn; function addSiteParentId(nodeId) { var theParentId = 'a'; var parentId = relationsManagerResource.GetParentId(nodeId) .then(function(response){ factoryReturn.theParentIdResult = response.data; console.log(theParentId);
Now we just need to make sure that the addSiteParentId method addSiteParentId always addSiteParentId before we access the theParentId property. We can achieve this using several methods.
then in the controller and other services used on your router, just call your_factory.theParentId to get your property. Contact here for more information: http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx
Use the run method of the application to enable your service.
app.run(function (your_factory) { your_factory.addSiteParentId(); })
Enter it in the first controller or controller services. In the controller, we can call all the necessary initialization services. Then all remaining controllers as children of the main controller can be accessed to this property as usual.
The choice of your methods depends on your context, depends on the scope of your variable and the frequency of reading of your variable.
Vu quyet
source share