Я создал простую директиву с функцией ссылки и контроллером.
Мне нужно получить доступ к набору свойств в области действия в контроллере, но он продолжает говорить undefined.
Директива:
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, я также получаю undefined ?!