使用带有angularjs的POST来发送信息

时间:2015-07-20 14:02:08

标签: angularjs post controller

我正在尝试从表单中检索信息并将其发送到网址,但我的方法似乎无法正常工作。

insert: function(id, callback) {
                    $http({
                        method: 'POST',
                        url: listofIds/id,
                        data: {
                            single_id : id
                        }
                    }).success(callback);
                }

然后在一个单独的.js文件中

.controller('IDkeyCtrl', function($scope) {

    $scope.update = function(newEntry) {
        console.log($scopeIDkey);
        $scope.id = $scope.IDkey;
    };

insert($scope.id);

我可以告诉我,我在这里错误地调用了函数insert()。有人可以帮助确定更好的方法吗?

1 个答案:

答案 0 :(得分:0)

尝试:

.controller('IDkeyCtrl', ['$scope', 'yourFactory', function($scope, yourFactory) {

    $scope.update = function(newEntry) {
        console.log($scopeIDkey);
        $scope.id = $scope.IDkey;
    };

    $scope.functionOnClick = function() {
        yourFactory.insert($scope.id, function(response) {
            //do something with response data
        });
    }

}])

.factory('yourFactory', ['$http', function($http) {

    return {
        insert: function(id, callback) {
            $http({
                method: 'POST',
                url: listofIds/id,
                data: {
                    single_id : id
                }
            }).success(function(response) {
                callback(response)
            });
        }
    };

}]);

<button ng-click="functionOnClick()">Your Button</button