Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to rename an Angular variable in the view?

In my Angular project, I am using a particular value that in my controller is called something like:

$scope.feature1.items.thisItem

There's a particular <div> in my view that uses thisItem many times and it's quite messy to be referring to it as {{feature1.items.thisItem}} for example:

<div id="{{feature1.items.thisItem}}header>
     <h1> You've reached the section about {{feature1.items.thisItem}} </h1>
</div>

Is there any way to rename this variable in the view? I would like to simply call it one. I've tried {{feature1.items.thisItem as one}} but that didn't work. Any other ideas?

like image 292
CodyBugstein Avatar asked Dec 17 '14 08:12

CodyBugstein


People also ask

How can you rename a variable?

To rename a variable:Right-click the variable and select Rename.

Can you rename an angular project?

Search in your project wherever the old name is used. In Visual Studio Code search can be done by using the search bar provided. Replace old name with your new name. Rename the project folder name as well and you are done.

How to declare variable in Angular template?

You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.


1 Answers

Yes! ng-init was designed for this very purpose - aliasing another variable:

<div ng-init="thisItem = feature1.items.thisItem">
     <h1> You've reached the section about {{thisItem}} </h1>
</div>
like image 123
pixelbits Avatar answered Sep 25 '22 16:09

pixelbits