推送到 dom-repeat 中使用的数据绑定数组(聚合物)
Pushing to data-bound array used in dom-repeat (Polymer)
我在自定义 Polymer 元素中有一个数据绑定数组 (dom-repeat
),我需要将新数据推送到数组中。它不显示项目,即使它知道已添加 2 个元素。我在这里错过了什么?
<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="main-element">
<template>
<ul>
<template is="dom-repeat" items="{{people}}">
<li>{{item.first}}</li>
</template>
</ul>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'main-element',
properties: {
people: {
type: Array,
value: function() {
return [];
}
}
},
ready: function() {
// Mock data retrieval
this.people.push({"first": "Jane", "last": "Doe"});
this.people.push({"first": "Bob", "last": "Smith"});
}
});
})();
</script>
将项目推入数组时使用 Polymer 的 array mutation methods:
this.push('people', {"first": "Jane", "last": "Doe"});
this.push('people', {"first": "Bob", "last": "Smith"});
<head>
<base href="https://polygit.org/polymer+1.7.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<main-element></main-element>
<dom-module id="main-element">
<template>
<ul>
<template is="dom-repeat" items="{{people}}">
<li>{{item.first}}</li>
</template>
</ul>
</template>
<script>
HTMLImports.whenReady(function() {
'use strict';
Polymer({
is: 'main-element',
properties: {
people: {
type: Array,
value: function() {
return [];
}
}
},
ready: function() {
// Mock data retrieval
this.push('people', {"first": "Jane", "last": "Doe"});
this.push('people', {"first": "Bob", "last": "Smith"});
}
});
});
</script>
</dom-module>
</body>
我在自定义 Polymer 元素中有一个数据绑定数组 (dom-repeat
),我需要将新数据推送到数组中。它不显示项目,即使它知道已添加 2 个元素。我在这里错过了什么?
<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="main-element">
<template>
<ul>
<template is="dom-repeat" items="{{people}}">
<li>{{item.first}}</li>
</template>
</ul>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'main-element',
properties: {
people: {
type: Array,
value: function() {
return [];
}
}
},
ready: function() {
// Mock data retrieval
this.people.push({"first": "Jane", "last": "Doe"});
this.people.push({"first": "Bob", "last": "Smith"});
}
});
})();
</script>
将项目推入数组时使用 Polymer 的 array mutation methods:
this.push('people', {"first": "Jane", "last": "Doe"});
this.push('people', {"first": "Bob", "last": "Smith"});
<head>
<base href="https://polygit.org/polymer+1.7.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<main-element></main-element>
<dom-module id="main-element">
<template>
<ul>
<template is="dom-repeat" items="{{people}}">
<li>{{item.first}}</li>
</template>
</ul>
</template>
<script>
HTMLImports.whenReady(function() {
'use strict';
Polymer({
is: 'main-element',
properties: {
people: {
type: Array,
value: function() {
return [];
}
}
},
ready: function() {
// Mock data retrieval
this.push('people', {"first": "Jane", "last": "Doe"});
this.push('people', {"first": "Bob", "last": "Smith"});
}
});
});
</script>
</dom-module>
</body>