我正在尝试创建一个测验指令,并且正在使用Angular UI Bootstrap的Carousel指令作为参考。
Angular UI Boostrap轮播使用外部“轮播”指令和内部“ slide”指令。我已经仔细阅读了代码和参考实现,但是在理解某些内容时遇到了麻烦。
外部标记(即页内原始,预渲染,预成角度的html)在对象数组上实现ng-repeat。
<carousel interval="myInterval">
<slide ng-repeat="slide in slides" active="slide.active">
<div class="carousel-caption">
<h4>Slide {{$index}}</h4>
<p>{{slide.text}}</p>
</div>
</slide>
</carousel>
我了解中继器在“幻灯片”中的工作方式是对$ scope.slides对象数组的引用。我不理解的是在模板中对“ slides()”的引用。数组如何成为函数?
该指令实现为:
.directive('carousel', [function() {
return {
restrict: 'EA',
transclude: true,
replace: true,
controller: 'CarouselController',
require: 'carousel',
templateUrl: 'template/carousel/carousel.html',
scope: {
interval: '=',
noTransition: '=',
noPause: '='
}
};
}])
下面是carousel.html上的模板。注意在li中继器中对“ slides()”的引用。
angular.module("template/carousel/carousel.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("template/carousel/carousel.html",
"<div ng-mouseenter=\"pause()\" ng-mouseleave=\"play()\" class=\"carousel\">\n" +
" <ol class=\"carousel-indicators\" ng-show=\"slides().length > 1\">\n" +
" <li ng-repeat=\"slide in slides()\" ng-class=\"{active: isActive(slide)}\" ng-click=\"select(slide)\"></li>\n" +
" </ol>\n" +
" <div class=\"carousel-inner\" ng-transclude></div>\n" +
" <a class=\"left carousel-control\" ng-click=\"prev()\" ng-show=\"slides().length > 1\"><span class=\"icon-prev\"></span></a>\n" +
" <a class=\"right carousel-control\" ng-click=\"next()\" ng-show=\"slides().length > 1\"><span class=\"icon-next\"></span></a>\n" +
"</div>\n" +
"");
}]);
我只是看了CarouselController中的代码,并看到了这些行。所以,那是从那里来的。谢谢pkozlowski.opensource的确认。:^)
var self = this,
slides = self.slides = [],
....;
$scope.slides = function() {
return slides;
};
这里没有魔术:-)该slides()
函数由carousel
指令的控制器显式地显示在作用域上:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句