$ scope.model值在AngularJS的控制器中取消定义

时间:2015-10-27 11:43:07

标签: angularjs

我是Angular的新人,最近我面临的问题是, 我无法获得价值:控制器中的ng-model值($ scope.yourAns,$ scope.questionID),一旦我提交表单,如果我能找到解决方案或帮助解决这个问题,我将很高兴。

下面是我的角度代码。

App.js

var wmlabApp = angular.module('wmlabApp', [
    'ngRoute',
    'ngMaterial',
    'labcatControllers',
]);
wmlabApp.config(['$routeProvider',
function($routeProvider) {
    $routeProvider.
    when('/questionList/:qId', {
        templateUrl: 'view/questionAndAns.html',
        controller: 'questionAndAnsCtrl'
    }).
    otherwise({
        redirectTo: '/library'
    });
}]);

questionAndAns.html

<div>
<div ng-repeat="item in items">

    <h1>{{item.question}}</h1>
    <hr />
    <div ng-if="item.answer">
        <h3>{{item.answer.length}} Answers</h3>
        <ul>
            <li ng-repeat="ans in item.answer">
                <h5>{{ans.aId}}</h5>
                <p id="ans.aId">{{ans.answers}}</p>
            </li>
        </ul>
        <hr />
    </div>

    <h3>Your Answer</h3>
    <div>
        <div id="messages" ng-show="message">{{ message }}</div>
        <form ng-submit="processForm()">
            {{item.qId}}
            <input type="hidden" ng-model="questionID" ng-value="questionID = item.qId" id="qID" ng-class="{ 'has-error' : errorqId }" name="questionID" />
            <span class="help-block" ng-show="errorqId">{{ errorqId }}</span>

            <textarea name="yourAns" ng-model="yourAns" ng-class="{ 'has-error' : errorAns }"></textarea>
            <span class="help-block" ng-show="errorAns">{{ errorAns }}</span>

            <input type="submit" id="submit" value="Submit" />
        </form>
    </div>

</div>

controller.js

labcatControllers.controller('questionAndAnsCtrl', ['$scope', '$http', '$routeParams',
function($scope, $http, $routeParams) {
    var param = {
        "qId": $routeParams.qId
    };

    $http.post('questionAndAns.php', param).success(function(data) {
        $scope.items = data;
    });
    console.log($scope.yourAns);
    console.log($scope.questionID);

    $scope.processForm = function() {
        var params = {
            "yourAns" : $scope.yourAns,
            "questionID" : $scope.questionID    
        }
        console.log(params)
        $http.post('setQuestionAns.php', params).success(function(data) {
                console.log(data);
                if (!data.success) {
                    $scope.errorAns = data.errors.ans;
                    $scope.errorqId = data.errors.qId;
                } else {
                    // if successful, bind success message to message
                    $scope.errorAns = ""
                    $scope.errorqId = ""

                    $scope.message = data.message;
                    $scope.formData.yourAns = "";
                    var param = {
                        "qId" : $scope.formData.questionID
                    }
                    $http.post('questionAndAns.php', param).success(function(data) {
                        $scope.items = data;
                    });
                }
            });
    };
}]);

0 个答案:

没有答案