【问题标题】:Angular4 ng-if condition whether select type of "input" or "select"Angular4 ng-if条件是选择“输入”还是“选择”类型
【发布时间】:2018-01-04 06:49:28
【问题描述】:

我是 Angular 的新手,无论我们选择 type=input 还是 type=select,我都在尝试使用 ng-if 条件,如果我们选择 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:5 ("]*ngIf="type='input'">

中的 [type='input'] 的第 6 列包含分配>

【问题讨论】:

    标签: angular typescript angular2-directives


    【解决方案1】:

    您应该使用 === 来签入您的模板

    `<div *ngIf="type==='input'">
    

    【讨论】:

    • 我用过。问题已解决。但屏幕上没有显示任何内容prntscr.com/hvuqpv 请参阅此链接
    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 2018-05-15
    • 2020-03-23
    • 1970-01-01
    • 2016-11-30
    • 2013-08-07
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多