javascript - 更改 {{variable}} 中的变量(不知道那叫什么)

标签 javascript jquery angularjs ionic-framework

我需要根据用户点击将变量“prefs”更改为“简单、中等或困难”。

示例: 这个

{{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'" 不起作用

感谢您提前提供帮助

编辑: 我尝试在 Controller 中访问的数组如下所示

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 由一个函数分配,该函数只需根据用户点击将 choice 设置为 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 (引用变量对象属性时,使用 [] 表示法而不是 . 表示法)

关于javascript - 更改 {{variable}} 中的变量(不知道那叫什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34303578/

相关文章:

jquery - 使用 CSS 和 Jquery 循环排列元素

javascript - 为什么我在 http 之后在 angularjs 变量中得到空值

javascript - 用于内部 Controller 的 Angularjs 只运行一次

jquery - 下载带有 HTTP "Authorization" header 集的文件

jquery - 在 jQuery 中将 sql 表的单个元素集成为变量

javascript - 当用户在输入文本框中键入内容时,绑定(bind)丢失的一种方式

javascript - 如何从具有 javascript 函数的页面获取 html?

javascript - 如何将此 jQuery 代码转换为 AngularJS 代码?

javascript - Meteor:显示以天分隔的文档

javascript - Google map 自定义内容框?