简体   繁体   English

控制器内部的angular-ui-router函数不是函数

[英]angular-ui-router function inside controller is not a function

I created a controller inside a state. 我在状态内创建了一个控制器。 We usually use this kind of notation for our angular (1.5) components and services with an angular.extend(self, {}) . 通常,我们对带有angular.extend(self, {}) angular(1.5)组件和服务使用这种表示法。

My problem here is when self.criteria is being initialized, the browser call self.getAgencies() and return an exception : 我的问题是在初始化self.criteria时,浏览器调用self.getAgencies()并返回异常:

Error: self.getAgencies is not a function 错误:self.getAgencies不是函数

(function (app) {
    'use strict';

    app.config(function ($stateProvider) {
        $stateProvider.state('app.invoice', {
            url: '/invoice'
            abstract: true,
            template: '<ui-view></ui-view>'
        })
        .state('app.invoice.list', {
            url: '/list?allMyParam',
            template: '<invoices criteria="$ctrl.criteria"></invoices>',
            controllerAs: '$ctrl',
            controller: function ($location) {
                var self = this;

                angular.extend(self,{
                    criteria: {
                        agencies: self.getAgencies()
                    },
                    getAgencies: function () {
                        if ($location.search().agencies) {
                            return undefined;
                        } else {
                            return ['foo', 'blah'];
                        }
                    }
                });
            }
        });
    });
})(angular.module('module', []));

I put getAgencies() function over the criteria prototype initialization but it did not change anything. 我将getAgencies()函数放在标准原型初始化上,但没有任何改变。

I got out of it by moving getAgencies() outside of angular.extend(self, {}) like this : 我通过将getAgencies() angular.extend(self, {})之外来angular.extend(self, {})这种情况:

var self = this;

var getAgencies = function () {
    if ($location.search().agencies) {
        return undefined;
    } else {
        return ['foo', 'blah'];
    }
}

angular.extend(self, {
    criteria: {
        agencies: getAgencies()
    }
});

My code is working so it is ok for me but I would like to understand why my self.getAgencies() is not working when this call inside a controller component works well, and make it better if I can. 我的代码可以正常工作,但是对我来说还可以,但是我想了解为什么当控制器组件内的此调用工作正常时,为什么我的self.getAgencies()无法正常工作,如果可以的话,请使其变得更好。

I'm using angular-ui-router 0.2.18 with angular 1.5.0. 我正在使用angular-ui-router 0.2.18和angular 1.5.0。 Thank you for your help. 谢谢您的帮助。

Because when this code is reached 因为到达此代码时

criteria: {
   agencies: self.getAgencies()
},

the angular.extend function has not been called yet, and there is no reason why self should contain the getAgencies function. angular.extend函数尚未被调用,并且没有理由为何self应该包含getAgencies函数。

Why not initialize the agencies afterwards? 为什么不随后初始化代理?

            angular.extend(self,{
                criteria: { },
                getAgencies: function () {
                    if ($location.search().agencies) {
                        return undefined;
                    } else {
                        return ['foo', 'blah'];
                    }
                }
            });

            self.criteria.agencies = self.getAgencies();

Alternatively, you could use a getter and post-pone calling the function: 另外,您可以使用getter和post-pone调用该函数:

        angular.extend(self,{
            criteria: { 
              get agencies() {
                if (!self._agencies) { 
                  self._agencies = self.getAgencies();
                }
                return self._agencies;
              }
            },
            getAgencies: ...
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM