change angular variable inside template

1,023 views
Skip to first unread message

iamas...@gmail.com

unread,
Mar 21, 2019, 6:01:37 AM3/21/19
to Angular and AngularJS discussion
hi,
I want a global variable in my component and then I want to change it in the HTML file. so I am looking for a way to change the component.ts variable inside the componrnt.html. is there any possible way to do that?
thanks in advance

Sander Elias

unread,
Mar 21, 2019, 12:00:47 PM3/21/19
to Angular and AngularJS discussion

Hi Iama,

You can assign whatever you want inside the template.

<p>{{ someVar = "1-2-3"}} </p>

Will do.
Or is that not really your question?
Regards
Sander

Aset Son

unread,
Mar 21, 2019, 10:00:44 PM3/21/19
to ang...@googlegroups.com
I've already tested this before and yet again I double checked it and it won't work:
Parser Error: Bindings cannot contain assignments at column 3  
<p>{{i="1"}}</p>
i is a global variable that I defined in the .ts file.


--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Mar 22, 2019, 2:51:25 AM3/22/19
to Angular and AngularJS discussion
Hi Aset,

Oh, right, you can do it when using props, but not in `{{}}`. I'm unsure what your purpose is. It looks like you are doing too much logic in your template.
If this is really a path you want to walk, you can use a helper.
Template:
```HTML
{{setProp('i',3)}}
```
controller:
```typescript
setProp(propName:string,val:any) { this[propName]=val, return val}
```

But this is really a sign that you are doing too much logic in the template, which will lead to hard to maintain and test results.

Regards
Sander

Reply all
Reply to author
Forward
0 new messages