我目前正在学习在AngularJs中使用指令,尽管我知道Angular 2.0仍然只是在alpha中,但我已经开始阅读关于web的“为角2.0准备”文章。将角度指令范围传递给指令控制器
其中有相当一部分提到了离开链接函数,并使用控制器和bindToController只是为了使指令更像Angular 2.0的“组件”。
我遇到的问题是,我真的不知道该怎么我的指令范围传递给我使用该指令控制器...
例如,给出以下指令时:
(function() {
'use strict';
angular.module('app').directive('gidsImagePreview', imagePreview);
/* @ngInject */
function imagePreview() {
var directive = {
restrict: 'EA',
templateUrl: 'scripts/directives/gidsImagePreview.directive.html',
scope : {
images : '='
},
controller: ImagePreviewController,
controllerAs: 'vm',
bindToController: true
};
return directive;
}
/* @ngInject */
function ImagePreviewController(){
var self = this;
self.featured = self.images[0];
self.preview = preview;
function preview(img){
self.featured = img;
}
}
})();
而下面的html“呼”的指令(vm.project.images是图像对象的数组与文件名属性):
<gids-image-preview images="vm.project.images"></gids-image-preview>
那么,是什么让我的“ SE ImagePreviewController中的“lf.images”总是未定义的?
阅读this article,他/她似乎是在做我在做什么(只是不能与数组对象)...
从我迄今为止所了解的情况来看,通过使用“bindToController”,变量可在控制器的“this”对象上使用,不需要$ scope。 – 2015-04-03 10:41:58