【发布时间】:2014-04-04 07:17:15
【问题描述】:
这是一个处理复选框不确定状态的指令:
.directive('ngIndeterminate', function() {
return {
restrict: 'A',
link: function(scope, element, attributes) {
attributes.$observe('ngIndeterminate', function(value) {
$(element).prop('indeterminate', value == "true");
});
}
};
})
然后,以这些数据为例:
$scope.data = [
{name: 'foo', displayed: 2, total: 4},
{name: 'bar', displayed: 3, total: 3}
];
你只需要:
<ul ng-repeat="item in data">
<li>
<input type="checkbox" ng-indeterminate="{{item.displayed > 0 && item.displayed < item.total}}" ng-checked="item.displayed > 0" />
{{item.name}} ({{item.displayed}}/{{item.total}})
</li>
</ul>
有没有什么方法可以编写没有双花括号的 ng-indeterminate 表达式,就像原生的 ng-checked 表达式一样?
ng-indeterminate="item.displayed > 0 && item.displayed < item.total"
我试过了:
.directive('ngIndeterminate', function($compile) {
return {
restrict: 'A',
link: function(scope, element, attributes) {
attributes.$observe('ngIndeterminate', function(value) {
$(element).prop('indeterminate', $compile(value)(scope));
});
}
};
})
但我收到以下错误:
Looking up elements via selectors is not supported by jqLite!
Here is a fiddle你可以玩。
【问题讨论】:
-
这样的? jsfiddle.net/d9rG7/1
标签: javascript angularjs checkbox angularjs-directive