我试图通过指令来处理水平照片滚动。 在链接中,我想测量容器以处理两个箭头以单击以创建上一个/下一个功能。 钍ECODE如下:Angularjs:如何访问局部或局部范围方法?
app.directive("photos", function() {
return {
restrict: "E",
replace: true,
scope: {
"photoid": "@",
"scrollable": "@",
"size": "@",
"height": "@",
"list": "=",
"prefix":"@",
"inline": "@",
"extension": "@",
"class": "@",
"slide": "@"
},
template:
'<div style="position:relative">\n\
<div id="photos{{photoid}}" class="scroller {{class}}" ng-class="[{{scrollable}}]" ng-style="{display:list.length==1?\'inline-block\':\'block\', width: list.length==1?\'{{size||\'171px\'}}\':\'auto\', height: \'{{height||size||\'171px\'}}\'}">\n\
<div ng-repeat="p in list"\n\
ng-style="{\'background-image\': \'url({{prefix}}{{p.file}}.thumb.{{extension||\'jpg\'}})\', width: \'{{size||\'171px\'}}\', height: \'{{height||size||\'171px\'}}\'}"\n\
ng-click="$parent.$parent.openPopoverImageViewer(\'#photos{{photoid}}\', {{$index}})">\n\
<div>{{p.text||p.description}}</div>\n\
</div>\n\
</div>\n\
<div class="prev" ng-if="list.length>1" nng-hide="slide<=0" ng-click="scope.prev()"></div>\n\
<div class="next" ng-if="list.length>1" nng-hide="slide>=list.length-1" ng-click="scope.next()"></div>\n\
</div>',
link: function (scope, el, attrs) {
//scope.list = JSON.parse(attrs.list);
//.animate({scrollLeft: x}, 200);
scope.slide = scope.slide||0;
console.log("scope.photos: entering for id = #photos"+scope.photoid);
var scroller = $("#photos"+scope.photoid);
var container = scroller.parent();
console.log("scope.photos: scroller = "+scroller);
console.log("scope.photos: container = "+container);
var w = container.width();
var h = container.height(); if (h>768) h = 768; // do not exceed image maximum retina size
console.log("scope.photos: container (w,h)=("+w+", "+h+")");
scope.scroller = scroller;
scope.container = {width: w, height: h};
scope.image = {width: h};
//scope.slides = c.length;
//scope.slide = index;
console.log("scope.photos: after sizing container is "+JSON.stringify(scope.container));
// make it scrollable or not
if (scope.list.length*h > w) {
scroller.addClass("hScrollable");
console.log("scope.photos: Scroller for "+scope.list.length+" added hScrollable class");
}
else {
scroller.removeClass("hScrollable");
console.log("scope.photos: Scroller for "+scope.list.length+" removed hScrollable class");
}
//console.log("scope.photos: Scroller for "+c.length+" photos (container w="+w+"px, image width="+h+") start scroll at index "+index+", scrolleft= "+x+" and got: "+$(scroller).scrollLeft());
var sw = container.width();
w = scroller.height();
var x = scope.slide*w; // offset at the left border
x -= (sw-w)/2; // center
if (x<0) x = 0; // make sure it's not underleft
scroller.scrollLeft(x);
//console.log("scope.photos: Scroller for "+c.length+" photos ("+w+"px width) start scroll at index "+index+", scrolleft= "+x+" and got: "+$(scroller).scrollLeft());
},
prev : function() {
console.log("scope.photos: prev()");
scope.scroller.scrollLeft(0);
},
next : function() {
console.log("scope.photos: next()");
scope.scroller.scrollRight(0);
}
};
});
不幸的是,它看起来像我无法访问宽度和我得到空容器的高度,如图控制台,如下所示:
[Log] scope.photos: entering for id = #photosImpacReglage (kws.corn.app.js.html, line 196)
[Log] scope.photos: scroller = [object Object] (kws.corn.app.js.html, line 201)
[Log] scope.photos: container = [object Object] (kws.corn.app.js.html, line 202)
[Log] scope.photos: container (w,h)=(null, null) (kws.corn.app.js.html, line 206)
[Log] scope.photos: after sizing container is {"width":null,"height":null} (kws.corn.app.js.html, line 212)
[Log] scope.photos: Scroller for 5 removed hScrollable class (kws.corn.app.js.html, line 221)
而且,我怎么能触发本地范围prev()和next()方法?当我点击方法中的console.log()时,我没有控制台消息?
[更新1] 感谢@ClintPowell我修改的指令和它的作品,除了更换:真。当我发表评论时,应用程序会中断。我不明白。
的的jsfiddle的权利有:enter link description here
你的'next'和'prev'方法不在本地范围内。你已经在指令中定义了它们。他们将无法访问。在'link'函数内部,你可以设置'scope.next = function(){...}'等等。没有足够的上下文让我们知道为什么'scroller.parent()。width()'是null。请张贴plunkr或jsfiddle。 – 2014-11-05 22:32:21
关于前/后,你把我放在正确的道路上。谢谢。 @ClintPowell – 2014-11-06 09:25:38
@ClintPowell我提供了一个小提琴。它几乎可以工作,除非我设置replace:true,那么它会中断。 – 2014-11-06 11:04:53