如何在angular值或angular常数内调用angular工厂



工厂:

app.factory('myGlobals', function ($http) {
    function getCountries() {
        var countries = [];
        $http.get('http://whatever/v1/countries').then(function (response) {
            countries = response.data;
        });
        return countries;
    }
    return {
        getCountries: getCountries
    }
});

角度模块中定义的常数

.constant ('SomeSchema', {
    type: 'object',
    properties: {
        host: {type: 'string', title: 'Host'},
        ip: {type: 'string', title: 'IP'},
        country: {
            type: 'string',
            title: 'Country',
            enum: getCountries
        },
        status: {type: 'boolean', title: 'Status'}
    },
    required : [
        "host","ip","country","status"
    ]
})

我应该怎么做,以便在上面的例子中使用例如getCountrys?(是的,它是一个常数,请忽略此问题。)

valueprovider的简写,具有预定义的$get函数,不能注入依赖项。factory也是简写,它可以注入依赖项。使用

.factory('SomeSchema', function (myGlobals) {
    return { ... };
})

是的,它是一个常数,请忽略这个问题。。

它不能被忽略,因为常量的独特之处在于它们可以在配置块中使用,而myGlobals工厂还不可用。

最新更新