【发布时间】:2018-05-27 06:26:54
【问题描述】:
我刚刚开始重构我的代码以在指令中执行 DOM 操作和函数,而不是像以前那样在控制器内部执行,但是我在访问使用控制器定义的变量/对象时遇到了问题控制器内的“this”语法我需要从那里继承它们。
我尝试使用如下的 bindToController,在其中添加了指令函数中使用的不同对象,但是当我尝试在“链接”中访问这些对象时,它们都以未定义的形式返回控制台。
这里的例子。在控制器中定义的“this.test”,尝试在控制台日志消息中的指令中访问它。
控制器:
app.controller('notificationsController', function($scope, $state, $http, $document, $mdDialog, $filter, $timeout, $mdToast) {
this.test = 'TEST';
指令:
app.directive('clearNotifications', function($mdDialog, $mdToast, $timeout) {
return {
controller: 'notificationsController',
controllerAs: 'notifications',
scope: {},
bindToController: {
notifications: '=',
filters: '=',
test: '@'
},
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
console.log('notifications.test string test: ' + notifications.test);
【问题讨论】:
标签: javascript angularjs model-view-controller angularjs-directive