我需要根据用户点击将变量“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.book1
或 allbooks.book2
所以我需要结合choice
和prefs
谢谢:)
最佳答案
你可以这样做:
<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/