Angular JS ng-controller无法处理页面加载

时间:2017-04-25 21:16:59

标签: angularjs servicenow

我有一个模块“testControllerModule”并在里面定义了控制器'myCtrl'。此代码显示带分页的表数据。页面加载时控制器无法工作/触发。

<div class="row">
    <div class="col-md-6" ng-controller="myCtrl">
      <h3>Open Incidents</h3>
      <table ng-table="tableParams" class="table table-condensed table-bordered table-striped">
        <tr ng-repeat="row in $data">
          <td data-title="'Name'" sortable="'name'">{{row.name}}</td>
          <td data-title="'Age'" sortable="'age'">{{row.age}}</td>
          <td data-title="'Money'">{{row.money}}</td>
        </tr>
      </table>
    </div>

  </div>

带控制器的模块:

(function() 
 {
    "use strict";
    var app=angular.module("testControllerModule", ["ngTable"]);
    app.controller("myCtrl", myCtrl);
    function myCtrl(NgTableParams) 
    {
        console.info('myCtrl init');
        var dataset = [{ name: 'christian', age: 21 , money:100}, { name: 'anthony', age: 88, money:200 }];
        this.tableParams = new NgTableParams({
            // initial sort order
            sorting: { name: "asc" } 
        }, {
            dataset: dataset
        });
    }
    myCtrl.$inject = ["NgTableParams"];


    angular.module("testControllerModule").run(configureDefaults);


    function configureDefaults(ngTableDefaults) {
        ngTableDefaults.params.count = 5;
        ngTableDefaults.settings.counts = [];
    }
    configureDefaults.$inject = ["ngTableDefaults"];
})();

2 个答案:

答案 0 :(得分:1)

你能试试<tr ng-repeat="row in dataset">吗 而不是

<tr ng-repeat="row in $data">

答案 1 :(得分:0)

你能尝试在控制器前加上' - ',比如

<div class="col-md-6" -ng-controller="myCtrl">

更新了答案

您可以修改以下代码吗?向控制器注入$ scope

    (function() 
 {
    "use strict";
    var app=angular.module("testControllerModule", ["ngTable"]);
    function myCtrl($scope,NgTableParams) 
    {

        var dataset = [{
                "name": "Test1",
                "age": 50,
                "money": 1000
              }, {
                "name": "Test2",
                "age": 51,
                "money": 1000
              }, {
                "name": "Test3",
                "age": 52,
                "money": 1000
              }, {
                "name": "Test4",
                "age": 53,
                "money": 1000
              }, {
                "name": "Test5",
                "age": 54,
                "money": 1000
              }, {
                "name": "Test6",
                "age": 55,
                "money": 1000
              }, {
                "name": "Test2",
                "age": 51,
                "money": 1000
              }, {
                "name": "Test3",
                "age": 52,
                "money": 1000
              }, {
                "name": "Test4",
                "age": 53,
                "money": 1000
              }, {
                "name": "Test5",
                "age": 54,
                "money": 1000
              }, {
                "name": "Test6",
                "age": 55,
                "money": 1000
              }, {
                "name": "Test2",
                "age": 51,
                "money": 1000
              }, {
                "name": "Test3",
                "age": 52,
                "money": 1000
              }, {
                "name": "Test4",
                "age": 53,
                "money": 1000
              }, {
                "name": "Test5",
                "age": 54,
                "money": 1000
              }, {
                "name": "Test6",
                "age": 55,
                "money": 1000
              }, {
                "name": "Test2",
                "age": 51,
                "money": 1000
              }, {
                "name": "Test3",
                "age": 52,
                "money": 1000
              }, {
                "name": "Test4",
                "age": 53,
                "money": 1000
              }, {
                "name": "Test5",
                "age": 54,
                "money": 1000
              }, {
                "name": "Test6",
                "age": 55,
                "money": 1000
              }];

        $scope.tableParams = new NgTableParams({},{dataset: dataset});
        console.info('myCtrl end');
    }  
    myCtrl.$inject = ["$scope","NgTableParams"];
    app.controller("myCtrl", myCtrl);


    angular.module("testControllerModule").run(configureDefaults);
    function configureDefaults(ngTableDefaults) {
        ngTableDefaults.params.count = 5;
        ngTableDefaults.settings.counts = [];
    }
    configureDefaults.$inject = ["ngTableDefaults"];  
})();