在其范围之外获取ajax回调

时间:2014-05-11 06:04:59

标签: javascript angularjs

我想插入一条记录,后端将返回一个回调,以便我可以推送到前端。我把push放在$ http范围内,但$ scope.itemRecord返回undefined。另一种方式可能是将推送置于$ http的范围之外,但是如何才能获得推送的回调?

我的HTML

<input placeholder="Insert a new record.." type="text" class="form-control " ng-model="itemRecord" />

<button type="submit" class="btn btn-primary" ng-click="addRecord()">Add Record</button>

我的js

$scope.addRecord = function () {
    if ($scope.Input != '') {
        $http({
            url: "php/Insert.php",
            method: "POST",
            data: {
                'RecordName': $scope.itemRecord,
                 'userId': userId
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data, status, headers, config) {

            $scope.tasks.push({
                    "RecordName": $scope.itemRecord,
                    "uId": userId,
                    "recordId": data
            });

        }).error(function (data, status, headers, config) {});
    }
    $scope.itemRecord = "";
};

0 个答案:

没有答案