来自$ resource的异步数据的指令没有正确初始化

时间:2012-09-29 00:19:09

标签: angularjs

我正在尝试创建一个简单的轮播。源图像通过$resource获得。我需要一种表达方式来通知数据已经改变。我需要使用包含在ng-model属性中的数组中的第一张照片初始化图库。

代码:

app.directive('ngCarousel', function() {

    var template =  //...;

    return {
        restrict: 'E',
        require: 'ngModel',
        scope: {data:'='},
        compile:function(tElement, tAttrs, transclude) {
            tElement.html(template);

            //...

            return function(scope, element, attrs, ctrl) {

                //...

                attrs.$watch(scope.$modelValue, function() {
                    //initialize the template
                })
            }
        }
    }
});

1 个答案:

答案 0 :(得分:0)

  

我需要一种通知数据已发生变化的监视表达式。

向$ resource查询添加回调。即,而不是(我在这里猜测,因为你没有提供任何显示你如何使用$ resource的代码)

$scope.image1 = MyResourceWrapper.query()

改为:

MyResourceWrapper.query( function(data) {
   // Now you know the data has arrived.  Initialize your gallery, etc.
   ...
}

另见http://angular-ui.github.com/bootstrap/#/carousel