0

基本上在我的控制器中,我正在$http.get()加载一些 html 来设置“当前视图”。我的问题是我不知道如何用这个新的动态内容重新绑定 jQuery 事件。

现在,我已经采取了这样的措施:

$http.get('/someurl', {}).success(function(data){
  $scope.detailedView = data;
  // Now here comes the rebinding
  setTimeout(function(){
    // Use jquery to bind the new content
  }, 1500);
});

我一直在寻找解决方案,我发现的任何相关内容都指向使用指令。我已经研究过了,但我不知道指令将如何用于这样的事情。

请注意,如果没有超时,绑定会在动态内容实际位于 DOM 之前运行。我也尝试过在 $apply 运行后找到类似于挂钩的东西,但没有找到类似的东西。

4

1 回答 1

2

首先应该看看你用 jQuery 做的事情是否不能用 angular 来完成。

这是可以使用的最简单的指令版本:

<div ng-repeat="item in items" my-directive>Item {{$index+1}}</div>
app.directive('myDirective', function ($timeout) {
    return function (scope, element, attrs) {
        $timeout(function () {
            /* element is a jQuery object when jQuery loaded in page before angular,*/
            /* otherwise is a jQlite object that has many of same methods as jQuery*/
            element.css({ 'border': '1px solid green', 'margin': '10px 30px','padding':'5px'});
        }, 0);
    }
 });

这是一个使用长超时为使用该指令的重复项生成数据的演示:

http://jsfiddle.net/AXYGL/

于 2013-04-08T23:31:33.243 回答