AngularJs Grid - Not working when place the code under a event

前端 未结 3 1774
星月不相逢
星月不相逢 2021-01-27 13:43

Here is the code.

Working version - Grid displays and page load.



    

        
相关标签:
3条回答
  • 2021-01-27 14:05

    Your ng-grid is initializing before you instantiate $scope.gridOptions, as as such it throws the undefined error. Also, if you change a $scope property programatically, you may need to use $apply to get your changes to display on the templates.

    So what you need to do is make sure the data structures exist (even if they're empty) and call $apply when "starting" the data-grid:

    app.controller('MyCtrl', function ($scope) {
            $scope.myData = [];
            $scope.gridOptions = { data: 'myData' };
            $scope.LoadGrid = function () {
                alert('');
                $scope.myData = [{ name: "Moroni", age: 50 },
                     { name: "Tiancum", age: 43 },
                     { name: "Jacob", age: 27 },
                     { name: "Nephi", age: 29 },
                     { name: "Enos", age: 34}];
                $scope.$apply();
            }
        });
    

    Here is an edited plunkr from the ngGrid examples page, to show this working: http://plnkr.co/edit/SnJ9J0?p=preview

    0 讨论(0)
  • 2021-01-27 14:24

    Move the Tag at the end before the last (will also load faster).

    I presume the error is because the javascript is loading before the html is ready.

    Does the console error log says anything else?

    0 讨论(0)
  • 2021-01-27 14:29

    It's OK to define your gridOptions before the data actually gets there. That's the gorgeous part about the way Angular binds data - just set the data to the $scope object that will eventually have data & your grid will be smart enough to update itself by an internal $watch on the data item.

    0 讨论(0)
提交回复
热议问题