2015-09-01 24 views
7

Sử dụng Aurelia, có thể sử dụng tên biến để tham chiếu động tên thuộc tính của đối tượng mô hình không?Aurelia - Sử dụng biến làm tên thuộc tính trong ràng buộc dữ liệu

Javascript:

dow = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']; 
test = { 
    monday:  false, 
    tuesday: false, 
    wednesday: false, 
    thursday: false, 
    friday:  false, 
}; 

HTML:

<label repeat.for="day of dow"><input type="checkbox" class="form-control" checked.bind="test[day]" />${day}</label> 

Điều này dẫn đến: "Lỗi Loại: obj là undefined"

Trả lời

8

Theo Aurelia Documents:

Each item that is being repeated by the repeat attribute has several special contextual values available for binding:

$parent - At present, the main view model's properties and methods are not visible from within the repeated item. We hope to remedy this in an update soon. For the mean time, you can access that view-model with $parent.

$index - The index of the item in the array.

$first - True if the item is the first item in the array.

$last - True if the item is the last item in the array.

$even - True if the item has an even numbered index.

$odd - True if the item has an odd numbered index.

Nó hoạt động sau khi cập nhật HTML để tham chiếu parent.new_deal [ngày]:

<label repeat.for="day of dow"><input type="checkbox" class="checkbox-inline" checked.bind="$parent.new_deal[day]" />${day}</label> 
Các vấn đề liên quan