Angular js 指令控制器访问范围 属性 但 returns 未定义
Angular js directive controller access scope property but returns undefined
我已经构建了一个带有 link 函数和控制器的简单指令。
我需要访问控制器范围内的 属性 集,但它一直显示未定义。
指令:
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>'
}
}]);
这是指令的使用示例:
<calendar-select-filter value="filter_options.environment" filter-type="environment"></calendar-select-filter>
在控制器中我有两个 console.log。
控制台日志一打印:
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
但随后控制台打印了两个:
undefined
我需要访问指令控制器中的 "filterType",但是如何访问?
我可以看到作用域中的所有内容都嵌套在名为 k 的内容中,但是当我控制台记录 $scope.k 时,我也得到未定义的信息?!
你的控制器函数在你的 link 函数之前执行。
您没有像选项那样为 filterType 设置默认值。
在 link 函数运行之前,不会定义 fitlesType 值。为什么不在控制器函数中提供默认值?
我已经构建了一个带有 link 函数和控制器的简单指令。
我需要访问控制器范围内的 属性 集,但它一直显示未定义。
指令:
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>'
}
}]);
这是指令的使用示例:
<calendar-select-filter value="filter_options.environment" filter-type="environment"></calendar-select-filter>
在控制器中我有两个 console.log。
控制台日志一打印:
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
但随后控制台打印了两个:
undefined
我需要访问指令控制器中的 "filterType",但是如何访问?
我可以看到作用域中的所有内容都嵌套在名为 k 的内容中,但是当我控制台记录 $scope.k 时,我也得到未定义的信息?!
你的控制器函数在你的 link 函数之前执行。
您没有像选项那样为 filterType 设置默认值。
在 link 函数运行之前,不会定义 fitlesType 值。为什么不在控制器函数中提供默认值?