Angular.js: how to call a service once for all the app controllers

后端 未结 2 1941
醉酒成梦
醉酒成梦 2021-01-21 18:59

I don\'t know if this is even one of the Angular concepts or possible to do but i have a service that call the user information (name, id, age, ...):

.factory(\'         


        
2条回答
  •  温柔的废话
    2021-01-21 19:21

    //Assume Value1 and Service1 have been correctly injected
    if (Value1.data === null) {
         Your_Var = Service1.call;
    } else {
        Your_Var = Value1.data;
    } 
    

    For simplicity sake I would put that in a ternary expression when implementing.

    For DRY compliance.

    Going with DRY we need a SharedFactoryFuncion to handle $http requests

    Your_Var = Service1.call;
    //Inside Service1.call
    return Value1.data === undefined ? SharedFactoryFuncion.get("url") : Value1.data;
    

    There are numerous ways to handle the return of data from .get() so I won't go there as it's not pertinent.

提交回复
热议问题