我正在尝试模仿AngularJS中普通Javascript的while循环。我只是想根据数量输入的值调用createPlant()方法x次,当点击“添加”按钮时,如下所示(它添加到数组'植物'):
$("#add").on("click", function() {
var i = 0;
while (i < $("#pQuantity").val()) {
createPlant();
i += 1;
};
在Angular中我有:
HTML
<button ng-click="addPlant()" type="button">Add</button>
而且我不确定剧本的外观,但这是我错误的说法:
JS
var i = 0;
while (i < $("#pQuantity").val()) {
$scope.addPlant = function() {
var plant = {name: $("#pVariety").val()};
$scope.plants.unshift(plant);
};
i += 1;
};
答案 0 :(得分:1)
我支持@dfsq在评论中告诉你的内容。如果你使用角度,请不要使用jQuery ...释放角度功率! :P
对于您的解决方案,您应该执行以下操作:
你的JS:
var wstream = fs.createWriteStream('myOutput.txt');
[1,2,3,4].forEach(function(item){
wstream.write(item, '\n');
}.bind(this));
wstream.end();
HTML:
$scope.result="";
$scope.addPlant = function(){
for (i=0; i<$scope.someValue;i++){
//do something
$scope.result=$scope.result+i.toString()+",";
}
};