Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AngularJS在POST后显示数据_Javascript_Angularjs_Post - Fatal编程技术网

Javascript 使用AngularJS在POST后显示数据

Javascript 使用AngularJS在POST后显示数据,javascript,angularjs,post,Javascript,Angularjs,Post,我正在尝试在POST操作后显示我输入的数据。但它不起作用。我怎样才能做到这一点 这是我的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 cl

我正在尝试在POST操作后显示我输入的数据。但它不起作用。我怎样才能做到这一点

这是我的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>Namn</th>
            <th>Efternamn</th>
            <th>E-post</th>
         </tr>
    </thead>
        <tr ng-repeat="info in test.data"><td>{{info.namn}}</td><td>{{info.efternamn}}</td><td>{{info.email}}</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.name.$invalid && !userForm.name.$pristine, 'has-success' : userForm.name.$valid }">
            <label>Name</label>
            <input type="text" name="name" class="form-control" ng-model="form.name" required>
            <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">Fel namn</p>
        </div>

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

        </div>

        <div class="form-group" ng-class="{'has-error' : userForm.email.$invalid && !userForm.email.$pristine, 'has-success' : userForm.email.$valid && !userForm.email.$pristine}">
            <label>Email</label>
            <input type="email" name="email" class="form-control" ng-model="form.email">
            <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Ange korrekt e-post</p>
        </div>      

        <button type="submit" class="btn btn-primary">Lägg till</button>
    </form>
</div>
</div>
提交后,如何查看在输入字段中输入的数据?如您所见,我已尝试设置$scope.test=data,但不起作用。

请参见此处:


您需要为$http post调用创建一个工厂方法,该方法将在注入工厂时将数据返回给控制器

angular.module('your factory module', [])
    .factory('testFactory', function($http) {
        return {
            urMethod: function(callback) {
                return $http({
                   <ur http call parameters>
                }).
                success(function(data) {
                    callback(data);
                });                 
            }
        };

    });

你对数据有什么期望?你们在控制台里有什么?@ThomasP1988:正如我现在看到的,数据只是一个整数。但是我尝试设置$scope.test=$scope.form,但这也不起作用。不要忘记$scope。$apply在success callback.urMethod中,这是我的$scope.submitForm吗?你想根据你的首选项命名的方法的名称它是你将从你的controllerThanks调用的函数名。什么是?Yout http url和post数据,可以作为参数传递给工厂方法。。。
    as.controller('Test', function($scope, $http, $rootScope)
            {   

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

                $scope.form = {};

  $scope.submitForm = function(isValid) {
          if(isValid) 
            {   
              $http.post($rootScope.appUrl + '/nao/test', $scope.form)
                .success(function(data, status, headers, config) {
                  console.log(data); <-- data is object returned from server
                   $scope.datePosted = $scope.form; <-- $scope.form is your object posted to server

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

                  $scope.datePosted =$scope.form;
                });


            }
        };
            });
angular.module('your factory module', [])
    .factory('testFactory', function($http) {
        return {
            urMethod: function(callback) {
                return $http({
                   <ur http call parameters>
                }).
                success(function(data) {
                    callback(data);
                });                 
            }
        };

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

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

        $scope.form = {};

        $scope.submitForm = function(isValid) {
            if(isValid) 
            {   
                    testFactory.urMethod(function(data){
                       $scope.test = data;
            });
            }
        };
    }]);