Angular4 ng-if条件是选择“输入”还是“选择”类型

问题描述 投票:1回答:1

我是角度新手,我正在尝试使用ng-if条件我们选择type = input还是type = select,如果我们选择type = select然后选择html将会渲染,否则输入类型将呈现这里是我的代码

    template: `<div *ngIf="type='input'">
                    <div class='investecField textField'>
                        <div class='investecFieldIn'>
                            <label>
                                <span>{{value}}</span>
                                <input type="text" value="{{valueInput}}">
                            </label>
                        </div>
                    </div>
                </div>`
                +
                `<div *ngIf="type='select'">
                    <div class='investecField selectField'>
                        <div class='investecFieldIn'>
                            <label>
                                <span>{{selectValue}}</span>
                                <strong><i class='fa fa-sort-desc' aria-hidden='true'></i></strong>
                                <select>
                                    <option></option>
                                    <option>Option 1</option>
                                    <option>Option 2</option>
                                    <option>Option 3</option>
                                </select>
                            </label>
                        </div>
                    </div>
                </div>`

})

export class investecFieldComponent implements OnInit {
    @Input()
    set selectValue(name: string) {

    };
    @Input() value: string;
    @Input() valueInput: string;


    constructor(private elem: ElementRef, private renderer: Renderer2) {

    }
    ngOnInit() {

    }

}

在HTML中它是:

<investec-field [ngIf]="type=='input'"
                                    value="User Name"
                                    valueInput="">
                    </investec-field>

但是我在控制台错误中遇到错误:模板解析错误:解析器错误:绑定不能包含ng:///AppModule/investecFieldComponent.html@0中的[type ='input']中第6列的赋值:5(“] * ngIf = “类型= '输入'”>

angular typescript angular2-directives
1个回答
2
投票

您应该使用===检查模板

`<div *ngIf="type==='input'">
© www.soinside.com 2019 - 2024. All rights reserved.