use*_*254 8 angularjs checklist-model
我知道解决方案不是很漂亮,但是我正在使用的模板需要它.
现在它可以工作,当产品在列表中时,它会向我显示"已添加"的span元素.
<section ng-repeat="product in products">
<label>
<input type="checkbox"
checklist-model="foobar.products"
checklist-value="product.id">
{{ product.name }}
</label>
<div ng-repeat="current_item in my.profile.items">
<span ng-show="product.id==current_item.id">Added</span>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)
我想要的是,当复选框后面还出现"已添加"文本时,选中要选中的复选框.
我试过这样做:
<ng-checked="{{my.profile.items.array.thing}}">
Run Code Online (Sandbox Code Playgroud)
但那不起作用.因为数组中的产品如:
[{
id: 1, name:"Trinity",
id: 2, name:"Van Gogh",
id: 3, name:"Bonaqua",
}]
Run Code Online (Sandbox Code Playgroud)
这my.profile.items
是一个包含更多信息的数组.因为它是我存储它的多对多关系.
有没有办法做到这一点?我不介意一个肮脏的解决方案:P
我试过这个:
// NG-check function
$scope.checkStoredValues = function(my) {
// Loop trought my current stored items
angular.forEach(my.profile.items, function(value, key) {
// Loop trough the array
angular.forEach(value, function(value, key) {
console.error(key);
// If key == 'id'
if(key == 'id'){
// Push the value to the Array of the checkboxes,
// so it's checked
// # Docs: http://vitalets.github.io/checklist-model/
console.info(value); // Return's: integer
foobar.products.push(value); // Needs more then an integer I guess..
}
});
});
};
Run Code Online (Sandbox Code Playgroud)
返回: TypeError: Cannot read property 'push' of undefined
bum*_*mpy 10
将此功能添加到您的控制器:
$scope.isChecked = function(product) {
var items = $scope.my.profile.items;
for (var i=0; i < items.length; i++) {
if (product.id == items[i].id)
return true;
}
return false;
};
Run Code Online (Sandbox Code Playgroud)
并为您的HTML使用
<section ng-repeat="product in products">
<label>
<input type="checkbox"
ng-checked="isChecked(product)">
{{ product.name }}
</label>
<div ng-repeat="current_item in my.profile.items">
<span ng-show="product.id==current_item.id">Added</span>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15817 次 |
最近记录: |