我有 2 个作用域函数,并声明了一个全局作用域变量。 我已经在一个函数中对全局变量进行了更改,但它没有反射(reflect)在 angularjs 中的另一个函数中。
这种情况就像我在表中有一个复选框,并编写了一个函数,如果选中该复选框,则值将被推送到 $scope.selection=[];
。
我有另一个删除按钮,它正在调用删除函数,删除操作将使用数组中存储的值执行。但是当我给出 console.log( )
在删除函数中。请告诉我您的建议。
代码:
$scope.selectionlist = [];
$scope.selectitem = function(itemselected){
if(itemselected.isChecked === true) {
$scope.selectionlist.push(itemselected.xyz);
}
}
$scope.deletememo = function(){
console.log("selection inside deletememo",$scope.selectionlist.length);
}
最佳答案
虽然@denny jonh已经回答了你的问题,但如果你不明白,也可以引用我的回答
JS
var app=angular.module("tableApp",[]);
app.controller("tblCtrl",function($scope){
$scope.selectedItem=[];
$scope.tableValues=[{
"id":1,
"name" : "dave",
"country":"india",
"designation":"software dev"
},
{
"id":2,
"name" : "giri",
"country":"USA",
"designation":"QA"
},
{
"id":3,
"name" : "dora",
"country":"UK",
"designation":"UI"
}
]
$scope.change=function(data){
if(data.isChecked){
$scope.selectedItem.push(data);
}
else{
$scope.selectedItem.splice($scope.selectedItem.indexOf(data),1)
}
}
$scope.displaySelectedRow=function(){
if($scope.selectedItem==""){
console.log("Select a row atleast")
}
else{
console.log("The selected Row Value is: ",$scope.selectedItem);
}
}
})
工作中Link
关于javascript - 对函数内的全局变量所做的更改不可用于 angularjs 中的另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41239394/