简体   繁体   English

Angular 4中的多个指令

[英]Multiple directives in Angular 4

I'm trying to use multiple binding in Angular 4 with *ngIf and *ngFor but I have this error: 我正在尝试在带有* ngIf和* ngFor的Angular 4中使用多重绑定,但出现此错误:

ERROR IMAGE 错误影像

code : 代码:

   <agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
    <agm-marker *ngFor="let o of m" [latitude]="o.latitude" [longitude]="o.longitude" iconUrl='../../assets/img/icon-pin.png' *ngIf="(o.breakdown == false || o.on_service == false)" iconUrl='../../assets/img/pin-maintenance.png' ></agm-marker>
  </agm-map>

and I've tried also with this code but also error: 而且我也尝试使用此代码,但也出错:

   <agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
    <agm-marker *ngFor="let o of m" [latitude]="o.latitude" [longitude]="o.longitude" iconUrl='../../assets/img/icon-pin.png' *ngIf="(!o.breakdown || !o.on_service )" iconUrl='../../assets/img/pin-maintenance.png' ></agm-marker>
  </agm-map>

Try with using the <ng-container> tag. 尝试使用<ng-container>标记。 It's a non-esthetical tag which could be useful for doing multiple directives. 这是一个非美学标记,可能对执行多个指令有用。

In you code could become something like: 在您的代码可能会变成类似:

<agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
  <ng-container *ngFor="let o of m">
      <agm-marker [latitude]="o.latitude" [longitude]="o.longitude" iconUrl='../../assets/img/icon-pin.png' *ngIf="(o.breakdown == false || o.on_service == false)" iconUrl='../../assets/img/pin-maintenance.png' ></agm-marker>
   </ng-container>
  </agm-map>

Remember that with using a directive under another, you can access the father directive values. 请记住,在另一个指令下使用指令,您可以访问父指令的值。
I explain better. 我解释得更好。 In this example, from the internal agm-marker tag you can access "o" value from its father tag directive, which is "o" value used on ng-container 在此示例中,您可以从内部agm-marker标签访问其父亲标签指令中的“ o”值,即在ng-container上使用的“ o”值

Means that you can't use two structural directives on the same element - directives with prefix * . 意味着不能在同一元素上使用两个结构指令-前缀为*指令。 Separate them like this 像这样分开

<agm-marker *ngFor="let o of m" [latitude]="o.latitude" [longitude]="o.longitude" [iconUrl]='(o.breakdown == false || o.on_service == false) ? "../../assets/img/icon-pin.png" : "../../assets/img/pin-maintenance.png"'>

</agm-marker>

You can not use two structural directives on the same element. 您不能在同一元素上使用两个结构指令。

You need to wrap your element in another one. 您需要将元素包装在另一个元素中。

It's advised to use ng-container since it wont be rendered in DOM. 建议使用ng-container因为它不会在DOM中呈现。

<ng-container *ngFor="let o of m">
    <agm-marker *ngIf="(o.breakdown == false || o.on_service == false)" [latitude]="o.latitude" [longitude]="o.longitude" [iconUrl]='(o.breakdown == false || o.on_service == false) ? "../../assets/img/icon-pin.png" : "../../assets/img/pin-maintenance.png"'>

    </agm-marker>
</ng-container>

Multiple structure directives are not allowed on the same tag. 同一标签上不允许使用多个结构指令。

If you want to use multiple bindings of structure directive then you can follow these solutions... 如果要使用结构指令的多个绑定,则可以遵循以下解决方案...

Solution 1 解决方案1

<agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
  <agm-marker 
    ngFor let-o [ngForOf]="m"
    [latitude]="o.latitude" 
    [longitude]="o.longitude" 
    iconUrl='../../assets/img/icon-pin.png' 
    *ngIf="(o.breakdown == false || o.on_service == false)" 
    iconUrl='../../assets/img/pin-maintenance.png' >
  </agm-marker>

Solution 2 (ng-container do not render in HTML) 解决方案2 (ng-container不以HTML呈现)

<agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
 <ng-container *ngFor="let o of m">
   <agm-marker
    [latitude]="o.latitude" 
    [longitude]="o.longitude" 
    iconUrl='../../assets/img/icon-pin.png' 
    *ngIf="(o.breakdown == false || o.on_service == false)" 
    iconUrl='../../assets/img/pin-maintenance.png' >
   </agm-marker>
<ng-container>

As per your requirement, you need iconUrl attribute binding which is based on condition. 根据您的要求,您需要基于条件的iconUrl属性绑定。 so here you can use ternary operator like this... 所以在这里你可以像这样使用三元运算符...

<agm-map *ngFor="let m of dataBikes" [latitude]="lat" [longitude]="lng">
   <agm-marker
     [latitude]="o.latitude" 
     [longitude]="o.longitude" 
     iconUrl="{{(o.breakdown == false || o.on_service == false)?'../../assets/img/icon-pin.png':'../../assets/img/pin-maintenance.png'}}" 
    </agm-marker>
</agm-map>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM