我有以下情形:訪問的ListView模型
ListModel {
id: myModel
}
ListView {
id: listView
anchors.fill: parent
model: myModel
delegate: recipeDelegate
}
Component {
id: recipeDelegate
Item {
...
...
property var modelTag: row.model
ListView
{
id: row
delegate: recipeDelegateTags
width: parent.width
height: contentItem.height
model: ListModel {
id: myModel2
}
}
}
Component { // I need to access this
id: recipeDelegateTags
Item {
id: recipeTags
width: parent.width
height: 35
Text {
id: ic
font.family: fontAwesome.name
color: cor
font.pixelSize: 16
text: checked_ ? "\uf046" : "\uf096"
width: 15
anchors.left: parent.left
anchors.leftMargin: 50
anchors.verticalCenter: parent.verticalCenter
}
MediumText
{
anchors.verticalCenter: ic.verticalCenter
text: titulo
color: cor
font.family: localFont.name
anchors.left: ic.right
anchors.leftMargin: 10
}
}
}
我能夠基於myModel用js函數像這樣的附加數據:
function appendItem(a, b)
{
myModel.append({"titulo": a, "id": b})
}
的問題:我如何訪問myModel2來追加一些數據?
我已經試過以下,但沒有成功:
function adicionaTag(a, b, c, d)
{
for(var i = 0; i < myModel.count; i++) {
var elemCur = myModel.get(i);
if(a === elemCur.id) {
console.log("property", elemCur.modelTag);
}
}
}
你們能幫助我嗎?謝謝!
您wan't訪問'myModel2'?你嘗試過'myModel2.append()'嗎? – derM
@derM是的,我做到了。事實上,我曾嘗試: elemCur.modelTag.append(B,C,d) elemCur.myModel2.append(B,C,d) 並收到此錯誤:**類型錯誤:無法調用「追加'未定義** 我必須使用屬性才能訪問父模型中的項目,對嗎? –
在實例化之前,您無法訪問模型。如果你想通過'id'訪問一個對象,'id'需要在範圍內。所以 - 如果你在組件中有一個'id',並且多次實例化這個組件,'id'不會再是一個標識符,如果它們都處於同一個範圍內的話。因此,當你在其他文件中聲明瞭Component時,Component中的對象的id就會隱藏到這個Component外面的對象中。 – derM