更改{{variable}}中的一个变量(不知道它叫什么)

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

本文关键字:不知道 变量 什么 一个 variable 更改      更新时间:2023-09-26

我需要根据用户点击将变量"prefs"更改为"easy,medium,or hard"。

示例:这个

{{choice.prefs.title}}

需要更改为{{choice.easy.price}}{{choice.medium.price}}{{choice.hard.price}}

基于用户点击的按钮

<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>

手动键入{{choice.easy.price}}工作,但{{choice.prefs.price}}ng-click="pref='easy'"不工作

感谢提前提供的帮助

编辑:我试图在控制器中访问的阵列看起来像这个

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"}
},
}

选项由一个函数分配,该函数根据用户点击将选项简单地设置为allbooks.book1allbooks.book2

所以我需要结合choiceprefs

感谢:)

您可以执行以下操作:

<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>

并使用:$scope[prefs].title而不是$scope.prefs.title(引用变量对象属性时使用[]表示法,而不是.表示法)