[英]How to output two dynamic variables that depend on each other
So for example : 因此,例如:
Controller: 控制器:
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: HTML:
<button ng-click="bookchoice(book1)">Book1</button>
<button ng-click="booktype(e-book)">E-Book</button>
{{choice.type.price}} <---this does not work
So I would like to know how to combine two dependent dynamic variables to access a part of an array and output the information 所以我想知道如何结合两个相关的动态变量来访问数组的一部分并输出信息
Thanks:) 谢谢:)
Your AllBooks data is a bit weird. 您的AllBooks数据有点奇怪。 I am not sure if possible but I would reformat it like this. 我不确定是否可以,但是我会这样重新格式化。 Then you can easily access the data using the key. 然后,您可以使用键轻松访问数据。
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;}
You can access it like this 您可以像这样访问
<button ng-click="bookchoice('book1')">Book1</button>
<button ng-click="booktype('e-book')">E-Book</button>
{{choice[type].price}}
You might want to use quotes in your ng-click methods to avoid problems. 您可能需要在ng-click方法中使用引号,以避免出现问题。
Controller 调节器
$scope.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.choice.type = selectedType;
}
HTML HTML
<button ng-click="bookchoice(allBooks.book1)">Book1</button>
<button ng-click="bookchoice(allBooks.book2)">Book2</button>
<button ng-click="booktype(choice.e_book)">E-Book</button>
<button ng-click="booktype(choice.hardcover)">Hardcover</button>
{{choice.type.price}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.