Angular js用全局作用域绑定局部数组



我有一个角形。只有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();
});

$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();
};

相关内容

  • 没有找到相关文章

最新更新