2

We created a directive in Angular2 and have an input property that should receive a string. The problem is that, we are not able to pass a string.

If we try to do this:

<learning-requirements [title]="You should be able to!" [content]="requirements"></learning-requirements>

It does not work, so we have to do this, and then it works:

<learning-requirements [title]="'You should be able to!'" [content]="requirements"></learning-requirements>

That is our directive:

@Component({
    selector: "learning-requirements",
    directives: [IONIC_DIRECTIVES],
    templateUrl: "build/pages/learning/components/requirements/requirements.html"
})
export class RequirementsComponent {
    @Input() public title: string;
    @Input("content") public items: Array<Requirement>;
}

Any idea how to handle string whithout explicitly using single quotation?

2 Answers 2

2

Basically whenever you have attribute wrap with [] square bracket it is going to evaluated attribute value(expression) with context(this) of component.

Since you're directly passing a value you could directly specify title attribute without [] square bracket, which means that the value which you have passed inside attribute would not get evaluated against context(this) of component.

Markup

<learning-requirements 
  title="You should be able to!" 
  [content]="requirements">
</learning-requirements>
Sign up to request clarification or add additional context in comments.

1 Comment

@dag Glad to hear that it helped, Thanks and cheers, you could find same answer here
2

Declare string(Type) variable and use it as shown below,

<learning-requirements [title]="someVar" [content]="requirements"></learning-requirements>


export class AppComponent{
  someVar:string="You should be able to!";
}

3 Comments

Ok, I know this, but is there a way to tell the directive that it's a string to avoid single quotes?
I don't understand what are you trying to say. What's your point after this answer?
If you don't want to use variable, you have to pass it with single quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.