0

angularjs の pager ディレクティブに取り組んでいます。サンプル コレクション ($scope.installations) をハード コーディングするとすべて問題ありませんが、サーバーからデータをプルすると、ディレクティブの「オプション」プロパティが常に「未定義」になります。

これが私の指示です:

angular.module("qusion.ui.pager", [])
    .directive("qnPager", [
        function() {
            return {
                restrict: 'A',
                link: function(scope, element, attrs) {
                    var options = scope.$eval(attrs.qnPager);
                    var settings = {
                        source: null,
                        paging: null,
                        schema: {
                            page: 'PageNumber',
                            size: 'Size',
                            totalPages: 'TotalPages',
                            totalCount: 'TotalCount'
                        }
                    };

                    angular.extend(settings, options);


                    scope.$watch(settings.source, function(value) {
                        console.log(settings.source); // UNDEFINED ???
                    });

                }
            };
        }
    ]);

これが私がそれを呼んでいる方法です。

<ul qn:pager="{source: installations}">

そして最後に私のコントローラーサンプル

function MyController(Installation){
   $scope.installations = [];
   // get sample (this works)
   // $scope.installations = [{ID: 1, Title: 'Test 1'}, {ID: 2, Title: 'Test 2'}]
   // get from server (this don't work)
   Installation.getAll().then(function(data) {
       $scope.installations = data.Installations;
   });

}
4

1 に答える 1

0

これで問題が解決するかどうかを確認してください。

インストールに新しいデータアレイを割り当てる代わりに、つまり、代わりに$scope.installations = data.Installations;、同じアレイにデータを入力してみてください。インストールに新しいデータ配列を割り当てると、古い配列へのバインドが失われる可能性があります。つまり、ディレクティブは以前のデータ配列にバインドされたままになる可能性があります。

$scope.installations.length = 0
for(var i = 0; i < data.Installations.length; i++){
    $scope.installations.push(data.Installations[i]);
}

詳細については、 https://stackoverflow.com/a/12287364/215945を参照してください。

更新: angular.copy()を代わりに使用できます/使用する必要があります:

angular.copy(data.Installations, $scope.installations);

宛先がcopy()メソッドに指定されると、最初に宛先の要素が削除され、次にソースから新しい要素がコピーされます。

于 2013-01-17T04:20:23.240 に答える