gpt4 book ai didi

javascript - Angular js 将本地数组与全局范围绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 06:58:27 25 4
gpt4 key购买 nike

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

关于javascript - Angular js 将本地数组与全局范围绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com