Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php文件的AngularJS服务_Php_Json_Angularjs - Fatal编程技术网

php文件的AngularJS服务

php文件的AngularJS服务,php,json,angularjs,Php,Json,Angularjs,我正在尝试为angular创建一个服务,它应该从生成json的php获取数据。现在我的服务是这样的 fixEvents.service('contactService', function($http, $q) { this.getallContact = function() { var json = $q.defer(); $http.get('models/contact.getall.json') .success(func

我正在尝试为angular创建一个服务,它应该从生成json的php获取数据。现在我的服务是这样的

fixEvents.service('contactService', function($http, $q) {
    this.getallContact = function() {
        var json = $q.defer();
        $http.get('models/contact.getall.json')
            .success(function(data) {
                json.resolve(data);
            })
            .error(function() {
                json.reject();
            });
        return json.promise;
    };
});
fixEvents.controller('contactCtrl', function($scope, contactService) {
    $scope.title = "CONTACT";

    $scope.jsonContact = contactService.getallContact();

    $scope.showMessage = function() {
        alert($scope.jsonContact.length);
    }
});
我的控制器是这样的

fixEvents.service('contactService', function($http, $q) {
    this.getallContact = function() {
        var json = $q.defer();
        $http.get('models/contact.getall.json')
            .success(function(data) {
                json.resolve(data);
            })
            .error(function() {
                json.reject();
            });
        return json.promise;
    };
});
fixEvents.controller('contactCtrl', function($scope, contactService) {
    $scope.title = "CONTACT";

    $scope.jsonContact = contactService.getallContact();

    $scope.showMessage = function() {
        alert($scope.jsonContact.length);
    }
});

问题是我的jsonContact没有得到任何结果。它似乎没有定义。我做错什么了吗?顺便问一下,有没有更好的办法?谢谢你,丹尼尔

您必须使用
。然后
返回控制器以处理数据:

var jsonContactPromise = contactService.getallContact();
jsonContactPromise.then(function(data) {
    $scope.jsonContact = data
}, function(error) {
    console.log("ERROR: " + error);
});

嗯,从语法上看,它看起来不错。在你的
success
函数中加入一个快速日志语句,
数据是未定义的还是已填充的?认为你错过了
。然后在控制器中,查看这个Google组,了解这个确切的主题:非常感谢,这非常有用!顺便问一下,在那之后是否有使用错误的更改?@PacuraruDaniel——是的,我编辑了答案以包含错误回调