Determine the type of "input" or "select" based on the Angular4 ng-if condition

I am a beginner in Angular and I am currently experimenting with using the ng-if directive to conditionally render different HTML elements based on whether type=input or type=select is selected. If type=select is chosen, then a select dropdown will be rendered; otherwise, an input field will be displayed. Here is a snippet of my code:

    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() {

    }

}

And here is how it is used in the HTML template:

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

However, upon testing, I encountered an error in the console which reads: Error: Template parse errors: Parser Error: Bindings cannot contain assignments at column 6 in [type='input'] in ng:///AppModule/investecFieldComponent.html@0:5 ("]*ngIf="type='input'")

Answer №1

When validating in your template, it's best to utilize ===

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CORS policy is preventing a POST request from a local Angular application, even though it is successful when using Postman or Thunder Client

In my web development project, I am working on integrating a list of job data from a remote server API in JSON format into a mat-table component. The company provided me with a link to the necessary APIs and a sample template for testing requests. You can ...

Unable to display label in form for Angular 2/4 FormControl within a FormGroup

I'm having trouble understanding how to: Use console.log to display a specific value Show a value in a label on an HTML page Display a value in an input text field Below is my TypeScript component with a new FormGroup and FormControls. this.tracke ...

Utilizing ngModel in a form with a personalized control: Tips and Tricks

I developed a custom component to manage select boxes, but after submitting the form, the selected option does not appear in the console. What could be causing this issue and how can I resolve it? The 'testOption' array of objects is passed thr ...

What is the best way to showcase column data in a React table when the data within the column is an array of objects?

Utilizing a react table to showcase a data table. In the tags column, the goal is to display all tags present in the tags array of objects. Despite several attempts, no success has been achieved yet. Being new to working with tables, any guidance on a more ...

Efficiently linking styles through computation in Angular

So I've been working with Angular5 and I figured out how to bind an HTML element's style to a Boolean value. However, I'm struggling to find a way to do this for multiple styles simultaneously. For example, I have this code snippet that wor ...

What is the proper way to assign a class name to an animation state in Angular 2/4?

I am currently working with Angular Animations using version 4.1.3 Check out the code snippet below: @Component({ selector : 'my-fader', animations: [ trigger('visibilityChanged', [ state('true' , style({ opaci ...

What is the proper way to reference a different TypeScript type within comments or JSDoc?

While I am well-versed in Javadoc, there is a feature that allows you to create links referring to the documentation of another type, as discussed here: /** * some java thingy. see this other java thingy too {@link OtherThingy} */ public class Thingy { ...

"Troubleshooting: Module not found" (Getting started with Jest in a nested project connected to a shared directory)

I've recently taken over a project that contains the following folder structure: node_modules/ server/ ├── node_modules/ ├── src/ │ └── helpers/ │ ├── updateTransactions.ts │ └── updateTransactions.tes ...

How to access elements by their class name in Angular

Recently, I encountered a situation with this specific span element: <span *ngFor="let list of lists[0].question; let i = index" id="word{{ i }}" (click)="changestyle($event)" class="highlight"> {{ list}} < ...

Guide on combining and compressing multiple packages into a single file with systemjs-builder

I have an angular2 application that I am bundling using the configuration below. All relevant libraries are stored in the lib folder and bundled into the js folder. const Builder = require('systemjs-builder'); gulp.task('build-minify-angula ...

"Learn how to programmatically close all panels in an ngb-accordion component in Angular 2 Release 6, leaving only

I have recently begun working on an accordion and I am trying to figure out how to make the first panel expand while keeping the others closed. I attempted to use [closeOthers]="true" but it doesn't seem to be effective. Here is my HTML code: <di ...

Encounter the error message "Unable to resolve all parameters" after including the Interceptor

Currently, I am in the process of implementing HttpInterceptor functionality. However, upon adding it to @NgModule, I encountered the following error message in Chrome: Uncaught Error: Can't resolve all parameters for JwtInterceptor: (?, ?). at s ...

Improve the way you manage the active selection of a button

ts isDayClicked: { [key: number]: boolean } = {}; constructor() { } setSelectedDay(day: string, index: number): void { switch (index) { case 0: this.isDayClicked[0] = true; this.isDayClicked[1] = false; this.isDay ...

How can I designate the Angular version when using the ng new command?

As I embark on creating a fresh Angular project, my goal is to ensure that all Angular dependencies are sourced from the stable 7th version. However, it appears that when executing the ng new app command, Angular always retrieves the latest version. Examin ...

Classification of Function Data

file1.tsx const handleData = (dataEvent: SimpleUiEvent) => { DataCollection.sendEvent(dataEvent); }; <File2 key={card.title} triggerData={() => handleData(card.dataEvent)} {...card} /> file2.tsx const handleCardFlip = () => ...

The Elusive Glitch: IOS Encounter with Ionic 2

VIEW PROBLEM</p> I am currently developing an Ionic 2 application using Angular 2. Interestingly, I have encountered a peculiar issue that only occurs on one specific page of the app, but specifically on IOS devices. Strangely enough, only the visib ...

Combining rxjs mergeMap with a response that yields an array

Currently, I am working on an ajax request that retrieves an Array of links. My goal is to utilize this array to make separate ajax requests for each link and then combine all the responses together. Here is how I have begun: ajax.post( url, data ).pipe( ...

No test coverage report is being generated for Angular 9 in Istanbul due to its emptiness

When generating a report, I am encountering an issue where the files are listed but the percentages are not being filled in. Any insights on what might be causing this problem? Error message: An error occurred in Handlebars: Access has been denied to res ...

Enhancing a prototype instance in TypeScript while activating strict mode

When working with an instance named remote from a factory in a vendor script, I encountered the need to add my own methods and members to that instance. While seeking a solution, I came across an insightful response on extending this in a Typescript class ...

My customized mat-error seems to be malfunctioning. Does anyone have any insight as to why?

Encountering an issue where the mat-error is not functioning as intended. A custom component was created to manage errors, but it is not behaving correctly upon rendering. Here is the relevant page code: <mat-form-field appearance="outline"> < ...