简体   繁体   English

Angular JS将局部数组与全局范围绑定

[英]Angular js bind local array with global scope

I have an angular form. 我有一个角形。 only 2 input box. 仅2个输入框。 I am taking the values from input box and then saving them in an array. 我从输入框中获取值,然后将它们保存在数组中。

then the problem begins. 然后问题开始了。 I want to show the array wrapped with <pre></pre> tag how do I do that. 我想显示用<pre></pre>标签包裹的数组。 Code sample is like this. 代码示例就是这样。

<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>

Script: 脚本:

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

Move $scope.dataShow = set.toString(); 移动$scope.dataShow = set.toString(); inside the function and remove the return : 在函数内部并删除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();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM