Angular ng repeat和jQuery插件
我想变成一个有角度的指令。我做了以下工作:Angular ng repeat和jQuery插件,jquery,angularjs,angularjs-directive,angularjs-ng-repeat,Jquery,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我想变成一个有角度的指令。我做了以下工作: define(['directives/directives'], function(directives){ directives.directive('vivifysortable', ['$rootScope', function ($rootScope){ return { controller: function ($scope, $element, $attrs){ // here i pas
define(['directives/directives'], function(directives){
directives.directive('vivifysortable', ['$rootScope', function ($rootScope){
return {
controller: function ($scope, $element, $attrs){
// here i pasted definition of multiselectable,
//and multisortable from the above jsfiddle
},
link: function($scope, $element, $attrs) {
if ($scope.$last === true) {
setTimeout(function() {
angular.element($element).multisortable({
connectWith: ".product-backlog-column",
selectedClass: "ui-selected"
});
})
}
}
}
}]);
});
如果我有这样的东西,这个很好用
<ul class="product-backlog-column" vivifysortable >
// some hardcoded values for testing purposes
<li> test 1 </li>
<li> test 2 </li>
<li> test 3 </li>
</ul>
看起来,ng repeat给我带来了困难,但我不知道为什么,以及如何解决
下面是解决这个问题的方法(我不确定我是否正确创建了它,包括Angular onLoad和jQuery作为外部资源)。我通过添加
$timeout
解决了这个问题。重构后的代码如下所示:
link: function($scope, $element, $attrs) {
$timeout(function () {
angular.element($element).multisortable({
connectWith: ".product-backlog-column",
selectedClass: "ui-selected"
});
}, 0);
}
问题是,
$timeout
将停止执行指令,直到ng repeat
完成渲染。这是我初学者的解释。如果有更有经验的人能对此有更多的了解,请这样做。我通过添加$timeout
解决了这个问题。重构后的代码如下所示:
link: function($scope, $element, $attrs) {
$timeout(function () {
angular.element($element).multisortable({
connectWith: ".product-backlog-column",
selectedClass: "ui-selected"
});
}, 0);
}
问题是,
$timeout
将停止执行指令,直到ng repeat
完成渲染。这是我初学者的解释。如果有更有经验的人能更清楚地说明这一点,请这样做。你能用同样的代码做一个小摆弄吗?我刚刚添加了JSFIDLE,请看一看你能用同样的代码做一个小摆弄吗?我刚刚添加了JSFIDLE,请看一看