【发布时间】:2016-06-20 12:39:29
【问题描述】:
我不知道这个爱的代码到底发生了什么
模板:
<article class="thumb" ng-repeat="product in store.products">
<a href="{{product.largeImg}}" class="image" ><img ng-src=" {{product.smallImg}}" alt="" /></a>
<h2>{{product.index}}. {{product.title}}</h2>
<p>{{product.desc}}</p>
</article>
指令
mainApp.directive('contentdir', function () {
return {
restrict: 'E',
replace: true,
templateUrl: 'template.html',
controller: ['$scope', '$http', function ($scope, $http) {
$scope.store = {};
$http.get('/getTemplate').success(function (data) {
$scope.store.products = data;
});
}],
link: function ($scope, $element, attr) {
setTimeout(function () {$('#main').poptrox({
baseZIndex: 20000,
/*options of poptrox*/
});
}, 10);
}
};
});
html
<div id="main">
<contentDir></contentDir>
</div>
如果我在链接函数中删除 setTimeout,代码将无法正常工作。可以向我解释 AngularJs 使用此代码的工作原理。非常感谢。
【问题讨论】:
-
setTimeout 中的代码没有使用 $element 或与指令没有任何关系。为什么它在链接函数中?
-
使用 jquery 插件的模板效果,我尝试在 Angularjs 中使用它。
-
我认为 Muthukannan 所问的是为什么你的指令作用于它之外的元素,这并不反映指令的用途。你不应该删除
#maindiv 并且也许这样做:$element.poptrox()在指令中?
标签: javascript jquery angularjs angularjs-directive