angularjs:无法从工厂获取数据

时间:2016-05-26 10:46:13

标签: angularjs

我无法从工厂获取我的json数据并将其显示在表格中。 当我使用$scope对象时,它运行正常但我在官方网站上看到他们不建议再使用$scope。所以我在演示示例中建议使用this参数。现在我的代码不再有效了。

请参阅我的代码并在这方面帮助我:

HTML:

<body ng-app="admin">         
        <div ng-controller="controller1 as ctrl1">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>IP</th>
                                    <th>Time</th>
                                    <th>No. of times</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="iprow in ctrl1.ipLog">
                                    <td>{{iprow.ip}}</td>
                                    <td>{{iprow.time}}</td>
                                    <td>{{iprow.count}}
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <script src="../framework/angular/angular.min.js"></script>
        <script src="javascript/app.js"></script>
        <script src="javascript/controllers/profileController.js"></script>
    </body>

angular app.js

var admin = angular.module('admin', ['myController']);

admin.factory('simpleFactory', function ($http) {
    var ipLog = [];

    var factory = {};

    factory.getIpLog = function () {
        // Simple GET request example:
        return $http({method: 'GET', url: 'mysql-query.php'}).
                then(function successCallback(response) {
                    ipLog = response.data;
                    return ipLog;
                }, function errorCallback(response) {
                    console.log(response.data);
                    return ipLog;
                });
    };
    return factory;
});

angular profileController.js

var myController = angular.module('myController', []);

myController.controller('controller1', ['simpleFactory', function (factory) {
        this.ipLog = [];
        init();
        function init() {
            var myDataPromise = factory.getIpLog();
            myDataPromise.then(function (result) {
                // this is only run after getData() resolves
                this.ipLog = result;
            });
        }
    }]);

3 个答案:

答案 0 :(得分:1)

您的观点:

<body ng-app="admin">         
    <div ng-controller="controller1">
        ...
            <tr ng-repeat="iprow in ipLog">
        ...
</body>

工厂代码:

var admin = angular.module('admin', []);

admin.factory('simpleFactory', function ($http) {
var factory = {};

factory.getIpLog = function () {
    // Simple GET request example:
    return $http({method: 'GET', url: 'mysql-query.php'});
};
return factory;
});

抓住控制器内的因子模块。 控制器:

var myController = angular.module('myController', ['admin']);

myController.controller('controller1', ['simpleFactory', function (factory) {
    $scope.ipLog = [];
    function init() {
        var myDataPromise = factory.getIpLog();
        myDataPromise.then(function (result) {
            $scope.ipLog = result.data;
        });
    }
    init();
}]);

答案 1 :(得分:0)

app.js中的

factory.getIpLog = function () {
    // Simple GET request example:
    return $http({method: 'GET', url: 'mysql-query.php'});
};
在profileController.js

myController.controller('controller1', ['simpleFactory', function (factory) {
    this.ipLog = [];
    init();
    function init() {
        var myDataPromise = factory.getIpLog();
        myDataPromise.then(function (success) {
            this.ipLog = success;
        }, function(error){
            console.log(error);
        });
    }
}]);

答案 2 :(得分:0)

this this.ipLog=[]中,myController中的this.ipLog=result引用this但是当您将值分配给myController时,this.ipLog此处不会请参考var myController = angular.module('myController', []); myController.controller('controller1', ['simpleFactory', function (factory) { var fixedThis=this; fixedThis.ipLog = []; init(); function init() { var myDataPromise = factory.getIpLog(); myDataPromise.then(function (result) { // this is only run after getData() resolves fixedThis.ipLog = result; }); } }]); 。所以你的 <Target Name="TeamCity"> <Message Text="I am Running!"/> </Target> 总是一个空数组。

试试这段代码:

  <Target Name="TeamCity" DependsOnTargets="Build">
    <Message Text="I am Running!"/>
  </Target>

请尝试使用此代码并告知其是否有效。