Angular 2 * ngFor语法

时间:2016-03-26 12:52:24

标签: angular angular2-template

我的主要问题是尝试循环一定次数让我们说n,但ngFor只接受数组,例如:" #item of [1, 2, ..., n]" ,那么只使用项目计数循环的正确方法是什么(不创建只有数字1到n的无用数组)?

所以我开始阅读更多语法,然后我注意到:

  • *ngFor="#item of items;
  • *ngFor="#item in items;

所以""之间有什么区别? """什么是用例?它与我的原始案件有什么关系吗?

提前致谢。

3 个答案:

答案 0 :(得分:4)

没有

TypeExists defined on the interface IElasticClient

你必须使用*ngFor="#item in items; 。 早期版本需要of,但他们将其更改为符合https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for...of

应该是

in

目前*ngFor="let item of items; ..." 仅迭代数组。

答案 1 :(得分:2)

items确实引用#item集合的每个元素#item*ngFor是为in呈现的每个元素创建的局部变量。

Angular 1版本中有

ngFor,Angular2 public class SimUtil { public static boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) { String name; try { if (simID == 0) { name = "isms0"; } else if (simID == 1) { name = "isms1"; } else { throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values"); } try { Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class}); method.setAccessible(true); Object param = method.invoke(null, new Object[]{name}); if (param == null) { throw new RuntimeException("can not get service which is named '" + name + "'"); } method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", new Class[]{IBinder.class}); method.setAccessible(true); Object stubObj = method.invoke(null, new Object[]{param}); method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class); method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsText, sentIntent, deliveryIntent); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return true; } catch (ClassNotFoundException e) { Log.e("Exception", "ClassNotFoundException:" + e.getMessage()); } catch (NoSuchMethodException e) { Log.e("Exception", "NoSuchMethodException:" + e.getMessage()); } catch (InvocationTargetException e) { Log.e("Exception", "InvocationTargetException:" + e.getMessage()); } catch (IllegalAccessException e) { Log.e("Exception", "IllegalAccessException:" + e.getMessage()); } catch (Exception e) { Log.e("Exception", "Exception:" + e); } return false; } } 语法不支持它。

答案 2 :(得分:0)

用ES6角度的类似语法做了这个改变。 在ES6中,

for (var v of arr){
   console.log(v); // it will return value (not key like in for...in)
}

类似地,在Angular 2.0中,它们遵循相同的语法。

*ngFor="#item of items;

将返回值。

in位于角度1中,与for(var i in arr)相似。但在角度2.0中不再支持