简体   繁体   English

Angular JS指令控制器访问范围属性,但返回未定义

[英]Angular js directive controller access scope property but returns undefined

I have built a simple directive with a link function and a controller. 我用链接功能和控制器构建了一个简单的指令。

I need to access a property set in the scope in the controller but it keeps saying undefined. 我需要访问控制器范围内的属性集,但它一直说未定义。

The directive: 指令:

app.directive('calendarSelectFilter', ['$log', 'eventService', function( $log, eventService ) {
    return {
        restrict: 'E',
        scope: {
            startingValue: '=',
            selected: "=value",
            filterType: '='
        },
        controller: function( $scope ){

            var options =[];
            console.log($scope); //LOG ONE
            console.log($scope.filterType);  //LOG TWO
            switch( $scope.filterType ){
                case 'environment':{
                    console.log( 'fetching envOptions' );
                    options = eventService.getSchemaLabelsAsArray('envOptions');
                } break;
                case 'event-type':{
                    options = eventService.getSchemaLabelsAsArray('eventNames');
                } break;
            }

            $scope.options = options;
        },
        link: function( scope, element, attrs ){
            if( !attrs.filterType ){
                $log.error( 'No filterType passed to the calendarSelectFilter directive' );
            }
            scope.filterType = attrs.filterType;
            scope.selected = scope.startingValue;
        },
        template: '<select ng-model="selected" ng-options="option for option in options">{{option}}</select>'
    }
}]);

This is a use example of the directive: 这是伪指令的使用示例:

<calendar-select-filter value="filter_options.environment" filter-type="environment"></calendar-select-filter>

In the controller i have two console.log. 在控制器中,我有两个console.log。

Console log one prints: 控制台日志一打印:

k {$id: "007", $$childTail: null, $$childHead: null, $$prevSibling: null, $$nextSibling: null…}
$$asyncQueue: Array[0]
$$childHead: null
$$childTail: null
$$destroyed: false
$$isolateBindings: Object
$$listenerCount: Object
$$listeners: Object
$$nextSibling: a.$$childScopeClass.$$childScopeClass
$$phase: null
$$postDigestQueue: Array[0]
$$prevSibling: null
$$watchers: Array[6]
$id: "007"
$parent: a.$$childScopeClass.$$childScopeClass
$root: k
filterType: "environment"
options: Array[0]
selected: undefined
startingValue: undefined
this: k
__proto__: k

But then console two prints: 但是,然后控制台两个打印:

undefined

I need to access the "filterType" in the directives controller, but how? 我需要在指令控制器中访问“ filterType”,但是如何?

I can see that the scope has everything nested inside something called k, but when i console log $scope.ki also get undefined ?! 我可以看到该作用域具有嵌套在k内的所有内容,但是当我控制台登录$ scope.ki时,它也变得不确定?

Your controller function executes before your link function. 控制器功能在链接功能之前执行。

You don't have a default value for filterType like you do for options. 您没有filterType的默认值,就像选项一样。

The fitlesType value isn't defined until the link function runs. 在链接功能运行之前,未定义fitlesType值。 Why not provide a default value in your controller function? 为什么不在控制器功能中提供默认值?

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

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