将值推送到数组AngularJS

时间:2014-07-17 12:12:14

标签: javascript angularjs syntax-error

我试图用.push();将数据值推送到AngularJS中的数组,但我总是收到此错误消息:

Error: $scope.test.push is not a function

这是我的HTML:

<div ng-controller="Test">
<div class="container">
<div class="col-sm-9 col-sm-offset-2">

    <div class="page-header"><h1>Testar</h1></div>
    <table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Sträcka</th>
            <th>Tid</th>
         </tr>
    </thead>
        <tr ng-repeat="info in test"><td>{{info.stracka}}</td><td>{{info.tid}}</td></tr>
    </table>

    <form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate> <!-- novalidate prevents HTML5 validation since we will be validating ourselves -->
        <div class="form-group" ng-class="{'has-error' : userForm.stracka.$invalid && !userForm.stracka.$pristine, 'has-success' : userForm.stracka.$valid }">
            <label>Sträcka(m):</label>
            <input type="text" name="stracka" class="form-control" ng-model="form.stracka" required>
            <p ng-show="userForm.stracka.$invalid && !userForm.stracka.$pristine" class="help-block">Fel sträcka</p>
        </div>

        <div class="form-group" ng-class="{'has-error' : userForm.tid.$invalid && !userForm.tid.$pristine, 'has-success' : userForm.tid.$valid && !userForm.tid.$pristine}">
            <label>Tid:</label>
            <input type="text" name="tid" class="form-control" ng-model="form.tid" ng-minlength="3" ng-maxlength="8">
            <p ng-show="userForm.tid.$error.minlength" class="help-block">För kort</p>
            <p ng-show="userForm.tid.$error.maxlength" class="help-block">För långt</p>

        </div>
        <button type="submit" class="btn btn-primary">Lägg till</button>
    </form>
</div>
</div>
</div>

这是我的控制器:

as.controller('Test', function($scope, $http, $rootScope, testFactory)
{   

    $http.get($rootScope.appUrl + '/nao/test/test')
        .success(function(data, status, headers, config) {
            $scope.test = data.data;
    });

    $scope.form = {};

    $scope.submitForm = function(isValid) {
        if(isValid) 
        {   
            /*testFactory.testFactoryMethod(function($http) {
                $scope.test = data;
            });*/
            $http.post($rootScope.appUrl + '/nao/test', $scope.form)
            .success(function(data, status, headers, config) {
                console.log(data);
                $scope.test.push($scope.form);

            }).error(function(data, status) {

            });
        }
    };
});

任何人都可以帮助我并解释我收到此消息的原因吗?

3 个答案:

答案 0 :(得分:9)

尝试这样做:

$scope.test = [];
$scope.test.push($scope.form);

答案 1 :(得分:2)

试试这个:

as.controller('Test', function($scope, $http, $rootScope, testFactory)
{   

    $http.get($rootScope.appUrl + '/nao/test/test')
        .success(function(data, status, headers, config) {
            $scope.test = data.data;
    });

    $scope.form = {};

    $scope.submitForm = function(isValid) {
        if(isValid) 
        {   
            /*testFactory.testFactoryMethod(function($http) {
                $scope.test = data;
            });*/

            $http.post($rootScope.appUrl + '/nao/test', $scope.form)
            .success(function(data, status, headers, config) {
                console.log(data);
                $scope.test = $scope.test || []; 
                $scope.test.push($scope.form);

            }).error(function(data, status) {

            });
        }
    };
});

答案 2 :(得分:1)

首先创建一个数组 - $scope.usermsg = [];

然后推送价值 - $scope.usermsg.push($variable);