【发布时间】:2016-06-20 15:59:44
【问题描述】:
我正在研究是否可以在 1.4 中采用指令并尝试类似于 1.5 组件。我正在使用bindToController 和controllerAs 在我的指令中使用控制器而不是单独的控制器。我已经成功地导出为函数,但想看看我是否可以导出为一个类,看看是否有充分的理由这样做。我现在遇到bindToController 错误,代码如下:
export default class recordingMenuComponent {
constructor(RecordingCtrl) {
'ngInject';
this.restrict = 'E',
this.scope = {},
this.templateUrl = '/partials/media/recording/recording-menu.html',
this.controller = RecordingCtrl,
this.controllerAs = 'record',
this.bindToController = {
video: '='
}
}
RecordingCtrl($log, $scope, $state, $timeout, RecordingService) {
'ngInject';
const record = this;
Object.assign(record, {
recentCalls: record.recentCalls,
startRecording() {
let video = {
accountId: $scope.accountId,
title: record.sipUrl
};
RecordingService
.recordVideoConference(video, record.sipUrl, record.sipPin, 0)
.then(result => {
video.id = result.id;
$timeout(() => $state.go('portal.video', {videoId: video.id}), 500);
}, error => $log.error('Error starting the recording conference: ', error));
},
getRecentCalls() {
RecordingService
.recentVideoConferenceCalls()
.then(result => {
record.recentCalls = result;
}, error => $log.error('There was an error in retrieving recent calls: ', error));
}
});
}
static recordingFactory() {
recordingMenuComponent.instance = new recordingMenuComponent();
return recordingMenuComponent.instance;
}
}
然后导入:
import angular from 'angular'
import recordingMenuComponent from './recordingMenuComponent'
angular.module('recordingModule', [])
.directive(recordingMenuComponent.name, recordingMenuComponent.recordingFactory);
为了简洁起见,我省略了一些模块,这些模块与尝试将此指令转换为组件无关。请注意,我试图在.directive() 之前不使用.controller()。
当我尝试使用它时,我得到了这个错误:
angular.js:9490 Error: [$compile:noctrl] Cannot bind to controller without directive 'recordingMenuComponent's controller
我不确定我是否走在正确的轨道上,或者这不是正确的道路。
感谢您的帮助。
【问题讨论】:
标签: javascript angularjs angularjs-directive ecmascript-6