1

我有一个角形。只有2个输入框。我从输入框中获取值,然后将它们保存在数组中。

那么问题就开始了。我想显示用<pre></pre>标签包裹的数组我该怎么做。代码示例是这样的。

<input type="text" class="form-control" id="qus" placeholder="Enter Question" ng-model="qus">
<input type="text" class="form-control" id="op1" placeholder="Option 1" ng-model="op1">
<label><input type="checkbox" ng-model="correct1">Correct</label>

<button class="form-control btn btn-primary" ng-click = "save()">Save</button>

<pre  ng-bind="dataShow"></pre>

脚本:

var app = angular.module('qApp', []);
app.controller('qCtrl', function($scope) {
    var set = [];
    var op1 = [];
    $scope.save = function (){
        if($scope.correct1!==true){$scope.correct1=false;}      
        op1.push($scope.op1, $scope.correct1);
        var qus = [$scope.qus, op1];
        set.push(qus);
        console.log(qus);
        console.log(set);
        return set; 
    };
    $scope.dataShow = set.toString();
});
4

1 回答 1

0

在函数内部移动$scope.dataShow = set.toString();并删除return

$scope.save = function () {
    var set = [];
    var op1 = [];

    if ($scope.correct1 !== true) {
        $scope.correct1 = false;
    }

    op1.push($scope.op1, $scope.correct1);

    var qus = [$scope.qus, op1];
    set.push(qus);
    console.log(qus);
    console.log(set);
    $scope.dataShow = set.toString();
};
于 2015-09-02T19:52:50.347 回答