AngularJS对象数组ng-show或ng-if

时间:2016-12-08 01:48:21

标签: angularjs angularjs-ng-repeat

我需要显示一条消息"没有类别"所以即时使用这个:

import std.stdio;
import std.string;
void main(){

  char[] data;

  writeln("Hey Player ",1,". Would you like to make a guess?\n Type 'y' to guess or 'n' to continue ");
  write("> ");
  readf(" %s", &data);
  char guessY = data[0];

  while(guessY != 'y' && guessY != 'Y' && guessY != 'n' && guessY != 'N')
  {
    writeln(guessY, " ",data);
    writeln("");
    writeln("Hey Player ",1," enter the correct input please.\n Type 'y' to guess or 'n' to continue ");
    write("> ");
    readf(" %s", &data);
    guessY = data[0];
   //fflush(&guessY);
  }
  writeln(guessY, " ",data);
}

并且不起作用。

它是一个对象数组,所以在控制台中,如果我有2个类别,我可以看到:

<div ng-repeat="categoryItem in categories">
                 <div ng-show="categoryItem.length">No categories</div>

所以,当我删除所有类别时,我编写了一个简单的if:

Array[2]
0
:
Object
$$hashKey
:
"object:77"
created_at
:
"2016-12-07T19:45:29.997063"
created_by
:
"test"
id
:
39
name
:
"Category 1"
updated_at
:
"2016-12-07T19:45:29.997105"
updated_by
:
null
__proto__
:
Object
1
:
Object
$$hashKey
:
"object:78"
created_at
:
"2016-12-07T19:45:34.202915"
created_by
:
"test"
id
:
40
name
:
"Category 2"
updated_at
:
"2016-12-07T19:45:34.202947"
updated_by
:
null
__proto__
:
Object
length
:
2

它有效,但是视图中没有显示消息,我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果有0个类别,则不会显示该消息,因为该消息位于ng-repeat内。由于categories数组的长度为0,因此视图中将有0个项目。

放置&#34; 无类别&#34; ng-repeat之外的消息,如此:

<div ng-repeat="categoryItem in categories">
    <div>{{categoryItem.name}}</div>
</div>
<div ng-hide="categories.length > 0">No categories</div>

请注意,我使用ng-hide并检查categories数组的长度,以确定&#34; 无类别&#34;应显示消息。