从Angular.js向服务器发布数据

时间:2013-12-24 10:24:43

标签: javascript angularjs

我正在开发一个应用程序如下

function attendeeCtrl($scope, $http) {
    $scope.submit = function () {
        console.log($scope.noattendees);
        $http({
            method: 'POST',
            url: 'http://localhost:2000/data',
            data: {
                event: $scope.event,
                date: $scope.date,
                spentby: $scope.spentby,
                amountspent: $scope.amountspent
            },
        });
    };
}    


<div ng-app>
    <h2>Team share</h2>
    <div ng-controller="attendeeCtrl">
        <label for="amountspent">Amount spent(Rs)</label>
        <input type="text" name="amountspent" id="amountspent" ng-model="amountspent" />
        <br>
        <label for="noattendees">Number of attendees</label> <span name="noattendees" id="noattendees" ng-model="noattendees">{{remaining()}}</span>
        </br>
        <label for="amountshared">Amount Shared</label><span ng-model="amountshared" name="amountshared" id="amountshared">{{amountspent/(remaining()+1)}}</span>
        </br>
        <input type="submit" id="submit" value="Submit" ng-click="submit()" />
        <input type="reset" id="reset" value="Reset" />
    </div>
</div>

console.log($scope.noattendees)返回“未定义”

如何在submit方法中获取{{return()}}的值? 请建议

1 个答案:

答案 0 :(得分:0)

从服务器获取返回值:

$http({method: 'GET', url: '/someUrl'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });