AngularJS $ save()没有发布或工作

时间:2014-09-29 15:59:20

标签: angularjs

我正在尝试在单独的服务器上使用php api创建一个带有angularjs的小应用程序(在我的本地机器上)

按照在线教程,我有以下代码。当$ save()被调用时,我在服务器上复制了$ _POST,它总是为空的。但是,我的数据正在加载好,所以api连接很好。

你能告诉我哪里出错了吗?

passwordServices.factory('Password', ['$resource',
    function($resource){
        return $resource(api_base + 'passwords/index/', {}, {
            query: {method:'GET', params:{}, isArray:true}
        });
    }
]);

passwordControllers.controller('PasswordDetailCtrl', ['$scope', '$routeParams', '$http', 'State', 'Password',
    function($scope, $routeParams, $http, State, Password) {
        $scope.password = Password.get({passwordId: $routeParams.passwordId}, function(data) {
            $scope.password.Password.title = "test";
            $scope.password.$save();
        });
    }
]);

以下是我的上下文的Cakephp API代码

public function index(){
        $this->autoRender = false;
        $this->response->type('json');

        if ($this->request->is('post')){
            var_dump($_POST);
        }else{
            if(isset($_GET['passwordId'])){
                $results = $this->Password->find('first', array(
                    'conditions' => array(
                        'Password.id' => $_GET['passwordId']
                    ),
                    'contain'   => array(
                        'Provider',
                        'Type'
                    )
                ));
            }else{
                $results = $this->Password->find('all', array(
                    'contain'   => array(
                        'Provider',
                        'Type'
                    )
                ));
            }
            $this->response->body(json_encode($results));
        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个

服务 -

passwordServices.factory('Password', function($resource, $q) {
    var resource = $resource(api_base + 'passwords/index/', {}, {
    query: {
        method: 'GET',
        isArray: true,
        cache: false
    },
    save: {
        method: 'POST',
        isArray: false,
        cache: false
    }
});
return {
    getPasswords: function() {
        var deferred = $q.defer();
        resource.query({},
            function(response) {
                deferred.resolve(response);
            },
            function(response) {
                deferred.reject(response);
            }
        );
        return deferred.promise;
    },
    savePassword: function(someObj) {
        var deferred = $q.defer();

        resource.save({}, someObj,
            function(response) {
                deferred.resolve(response);
            },
            function(response) {
                deferred.reject(response);
            }
        );

        return deferred.promise;
    }
};
]);

控制器 -

passwordControllers.controller('PasswordDetailCtrl', ['$scope', '$routeParams', '$http', 'State', 'Password',
    function($scope, $routeParams, $http, State, Password) {

        $scope.handleGetPasswords = function (response) {
            $scope.password = response;
            $scope.password.Password.title = 'test';
            Password.savePassword($scope.password).then(function (response) {
                console.log(response);
            })
        };

        Password.getPasswords().then($scope.handleGetPasswords);

    }
]);