解析形式角度路由器将输入参数设置为“undefined”

时间:2018-04-01 14:14:10

标签: angularjs binding routing angular-ui-router

我厌倦了这个tutorial

app.config(['$stateProvider', '$urlRouterProvider', '$compileProvider',
    function ($stateProvider, $urlRouterProvider, $compileProvider) {

        self = this;
        $compileProvider.preAssignBindingsEnabled(true);

        $urlRouterProvider.otherwise('/');

        $stateProvider.state('home', {
            url: '/',
            //template: '<home-component></home-component>',
            component: 'homeComponent',
            params: {
                selectedFilter: undefined
            },
            resolve: {
                isAdOps: function () {
                    return false;
                }
            }
        })

(function (app) {
    app.component('homeComponent', {
        templateUrl: 'partials/home-partial.html',
        bindings: {
            isAdOps: '<'
        },
        controller: ['$scope', '$state', function ($scope, $state) {

            var self = this;

            console.log("self.isAdOps = " + self.isAdOps);
            self.isFullList = false;

            // this.$onChanges = function (isAdOps) {
            //     if (angular.isDefined(isAdOps)) {
            //
            //         self.isAdOps = isAdOps;
            //         console.log("self.isAdOps = " + self.isAdOps);
            //         $scope.isAdOps = !self.isAdOps ? true : self.isAdOps;
            //
            //     }
            // };

            self.addVoice = function () {
                $state.go("add");
            };

            $scope.$broadcast('searchNoFilter');
        }]
    });

})
(promptoWeb);

但我仍然进入控制台:self.isAdOps = undefined

缺少什么?

注入控制器也不起作用:

angular 1.5 passes undefined value to a controler after state.Provider "resolve"

1 个答案:

答案 0 :(得分:-1)

当你解决问题时,可以注入它。它不会自动添加到控制器作为属性:

 controller: ['$scope', '$state', 'isAdOps', function ($scope, $state, isAdOps) {

            var self = this;

            self.isAdOps = isAdOps;
            console.log("self.isAdOps = " + self.isAdOps);