我試圖通過指令來處理水平照片滾動。 在鏈接中,我想測量容器以處理兩個箭頭以單擊以創建上一個/下一個功能。 釷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