如何使用 ngIf 确定字符串对象数组是否为空?
How to determine if an array of string objects is empty using ngIf?
我必须编写一段代码,其中我必须在两个列表之间传输一些项目,并在要传输到的对象文字数组不为空时隐藏错误。我创建了两个控制器(管理两个单独的列表)和一个服务来处理公共数据。服务内部定义了一个列表以及一些将项目从一个列表传输到另一个列表的函数。数组大小似乎从未从 0 改变,这是我试图在 ngIf 中使用的逻辑。
我的逻辑是检查数组是否为空,然后 return 如果数组为空则返回真值并在控制器中设置一个空变量。然后在 ng-if 中我将检查 ng-if="b.empty" 并认为那会起作用但它没有。在我的代码的整个生命周期中,数组大小都将保持为 0。我使用了 ._isEmpty(list),angular([],[]) 和最明显的 array.length 但问题最初是它们显示 0,但随后数组大小从未改变。即使在填充目标数组之后,大小似乎仍为 0,上面 functions/methods 的 any/all。
l1.$inject = ['listService']
function l1(listService){
var buying = this;
buying.items = listService.display();
buying.Add = function ($index){
listService.addItem($index);
}
}; //This is the controller for the source array.
.
.
.
bought.empty = listService.checkIfFull(); //Part of the second controller which assigns empty a boolean value
.
.
.
service.checkIfFull = function (){
if(blist.length == 0){
console.log(_.isEmpty(blist))
console.log(tblist)
return false;
}
else
{
console.log("not going here");
return true;
}
}; //The service checks if the array is empty
<div class="emptyMessage" ng-if="b.empty">Nothing bought</div>
console.log 语句的值似乎也只在 if 语句的真实部分执行。我为此找到了一个解决方案,如果本地列表的(我正在循环其中的 ng-repeat)长度等于零并且有效,那么只需检查 html 标签本身。但是你能解释一下为什么我的尝试是错误的吗?我是 AngularJs 和 JS 的初学者,所以我可能不了解有关 js 的一些规则,因此写了错误的代码。谢谢。
编辑:这是代码笔的 link-> https://codepen.io/meanmanmachineman/pen/RmmdjY
您的问题是由行 bought.empty = listService.checkIfFull();
引起的。您正在调用函数 listService.checkIfFull()
并将返回值分配给 bought.empty
。您应该做的是将函数本身分配给 bought.empty
:
bought.empty = listService.checkIfFull;
这样每次计算 bought.empty
时,它 returns 当前的实际值。
编辑:
我会尝试更明确地说明 bought.empty = listService.checkIfFull()
和 bought.empty = listService.checkIfFull
之间的区别。
第一种方法,bought.empty
将调用 listService.checkIfFull()
并将返回值存储为静态值,任何时候对变量求值,该值都是相同的。
通过使用另一种方法,bought.empty
的值不是数字而是 listService.checkIfFull
函数本身。这样,每次AngularJS对变量求值,函数都会执行,returns对应的值。
我必须编写一段代码,其中我必须在两个列表之间传输一些项目,并在要传输到的对象文字数组不为空时隐藏错误。我创建了两个控制器(管理两个单独的列表)和一个服务来处理公共数据。服务内部定义了一个列表以及一些将项目从一个列表传输到另一个列表的函数。数组大小似乎从未从 0 改变,这是我试图在 ngIf 中使用的逻辑。
我的逻辑是检查数组是否为空,然后 return 如果数组为空则返回真值并在控制器中设置一个空变量。然后在 ng-if 中我将检查 ng-if="b.empty" 并认为那会起作用但它没有。在我的代码的整个生命周期中,数组大小都将保持为 0。我使用了 ._isEmpty(list),angular([],[]) 和最明显的 array.length 但问题最初是它们显示 0,但随后数组大小从未改变。即使在填充目标数组之后,大小似乎仍为 0,上面 functions/methods 的 any/all。
l1.$inject = ['listService']
function l1(listService){
var buying = this;
buying.items = listService.display();
buying.Add = function ($index){
listService.addItem($index);
}
}; //This is the controller for the source array.
.
.
.
bought.empty = listService.checkIfFull(); //Part of the second controller which assigns empty a boolean value
.
.
.
service.checkIfFull = function (){
if(blist.length == 0){
console.log(_.isEmpty(blist))
console.log(tblist)
return false;
}
else
{
console.log("not going here");
return true;
}
}; //The service checks if the array is empty
<div class="emptyMessage" ng-if="b.empty">Nothing bought</div>
console.log 语句的值似乎也只在 if 语句的真实部分执行。我为此找到了一个解决方案,如果本地列表的(我正在循环其中的 ng-repeat)长度等于零并且有效,那么只需检查 html 标签本身。但是你能解释一下为什么我的尝试是错误的吗?我是 AngularJs 和 JS 的初学者,所以我可能不了解有关 js 的一些规则,因此写了错误的代码。谢谢。
编辑:这是代码笔的 link-> https://codepen.io/meanmanmachineman/pen/RmmdjY
您的问题是由行 bought.empty = listService.checkIfFull();
引起的。您正在调用函数 listService.checkIfFull()
并将返回值分配给 bought.empty
。您应该做的是将函数本身分配给 bought.empty
:
bought.empty = listService.checkIfFull;
这样每次计算 bought.empty
时,它 returns 当前的实际值。
编辑:
我会尝试更明确地说明 bought.empty = listService.checkIfFull()
和 bought.empty = listService.checkIfFull
之间的区别。
第一种方法,bought.empty
将调用 listService.checkIfFull()
并将返回值存储为静态值,任何时候对变量求值,该值都是相同的。
通过使用另一种方法,bought.empty
的值不是数字而是 listService.checkIfFull
函数本身。这样,每次AngularJS对变量求值,函数都会执行,returns对应的值。