简体   繁体   中英

Change a variable within a {{variable}} (don't know what that is called)

I need to change the variable "prefs" to either "easy, medium, or hard" based on user click.

Example: THIS

{{choice.prefs.title}}

NEEDS TO CHANGE TO {{choice.easy.price}} or {{choice.medium.price}} or {{choice.hard.price}}

based on which button is clicked by the user

<button class="button button-stable" ng-click="prefs="easy"")>Change to Easy</button>
</div>

<button class="button button-stable" ng-click="diff='normal'">Change to Medium</button>
</div>

<button class="button button-stable" ng-click="diff='hard'">Change to Hard</button>
</div>

Manually typing {{choice.easy.price}} works but {{choice.prefs.price}} and ng-click="pref='easy'" does not work

Thanks for the help in advance

EDIT: The array I am trying to access in my controller looks like this

allbooks={
book1:{
easy{title:"whatever"},
medium{title:"hello"},
hard{title:"another"}
},
book2:{
easy{title:"whatever"},
medium{title:"hello"},
hard{title:"another"}
},
book3:{
easy{title:"whatever"},
medium{title:"hello"},
hard{title:"another"}
},
}

Choice is assigned by a function that simply sets choice to allbooks.book1 or allbooks.book2 based on user clicks

So I need to combine choice and prefs

Thanks:)

You may do something like this:

<button class="button button-stable" ng-click="prefs='easy'")>Change to Easy</button>
</div>

<button class="button button-stable" ng-click="prefs='normal'">Change to Medium</button>
</div>

<button class="button button-stable" ng-click="prefs='hard'">Change to Hard</button>
</div>

and use: $scope[prefs].title instead of $scope.prefs.title (Use [] notation instead of the . notation when referencing variable object properties)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM