例如:
控制器:
AllBooks[{
book1:{
hardcover{price:25.99},e-book{price:1.99}
}
},
book2:{
hardcover{price:60.00},e-book{price:2.99}
}];
$scope.bookchoice = function(selectedBook) {
$rootScope.choice = selectedBook;}
$scope.booktype = function(selectedType) {
$rootScope.type = selectedType;}
HTML:
<button ng-click="bookchoice(book1)">Book1</button>
<button ng-click="booktype(e-book)">E-Book</button>
{{choice.type.price}} <---this does not work
所以我想知道如何组合两个相关的动态变量来访问数组的一部分并输出信息
感谢:)
答案 0 :(得分:0)
您的AllBooks数据有点奇怪。我不确定是否可能,但我会像这样重新格式化。然后,您可以使用密钥轻松访问数据。
AllBooks = {
book1:{hardcover{price:25.99},e-book{price:1.99}},
book2:{hardcover{price:60.00},e-book{price:2.99}}
};
$scope.bookchoice = function(selectedBook) {
$rootScope.choice = AllBooks[selectedBook];}
$scope.booktype = function(selectedType) {
$rootScope.type = selectedType;}
您可以像这样访问它
<button ng-click="bookchoice('book1')">Book1</button>
<button ng-click="booktype('e-book')">E-Book</button>
{{choice[type].price}}
您可能希望在ng-click方法中使用引号以避免出现问题。