1

我的 angularjs 有问题。我正在尝试添加 ui-scroll,但是当我这样做时,我遇到了这个错误。

也许是因为 html 调用它时没有加载数据。

错误:$injector:unpr

未知提供者:datasourceProvider <- datasource

这是我的服务

app.factory('actionScroll', function ($http, $timeout,$q) {
    return {
        default: {
            first: 0,
            max: 5,
            delay: 100
        },
        data: [],
        init: function (donne) {
            this.first = this.default.first;
            this.max = this.default.max;
            this.delay = this.default.delay;

            for (var i = this.first; i <= this.max; i++) {
                this.data[i] = {
                    index: i,
                    content: donne[i]
                };
            }
        },

        request: function (index, count) {
            var self = this;
            var deferred = $q.defer();

            var start = index;
            var end = index + count - 1;

            $timeout(function () {
                var item, result = [];
                if (start <= end) {
                    for (var i = start; i <= end; i++) {
                        if (item = self.data[i]) {
                            result.push(item);
                        }
                    }
                }
                deferred.resolve(result);
            }, self.delay);

            return deferred.promise;
        }
    }
})

这是我的 app.js

$scope.list= function () {
    $http.get(requestURL).then(function (response) {
        actionScroll.init(response.data.Liste)
        $scope.datasource = {
            get: function (index, count, success) {
                console.log('requested index = ' + index + ', count = ' + count);
                actionScroll.request(index, count).then(function (result) {
                    console.log('resolved ' + result.length + ' items');
                    success(result);
                });
            }
        };
    })
}

$scope.list()

这是我的html

<ul class="viewport" ui-scroll-viewport>
    <li ui-scroll="item in datasource" adapter="adapter" buffer-size="5">
        <span class="title">{{item}}</span>
    </li>
</ul>
4

1 回答 1

1

也许只有在数据可用/从 api 返回时才尝试加载组件:

$scope.list= function () {
    $scope.isDataLoaded= false;
    $http.get(requestURL).then(function (response) {
        $scope.isDataLoaded= true
    ...
})
<ul ng-if="isDataLoaded" class="viewport" ui-scroll-viewport>
    <li ui-scroll="item in datasource" adapter="adapter" buffer-size="5">
        <span class="title">{{item}}</span>
    </li>
</ul>
于 2022-02-28T15:29:48.867 回答