Javascript angularjs是否可以将模型映射到;“虚拟模型”;用过滤器?

Javascript angularjs是否可以将模型映射到;“虚拟模型”;用过滤器?,javascript,angularjs,Javascript,Angularjs,我在Angular js中放了两个模型 // full device list $scope.devices = [{id:1, on:true}, {id:2, on:false}, {id:3, on:true}]; // devices with on == true $scope.devicesOn = $scope.devices.filter(function(t){return t.on==true}); 第页: <span>the following {{ devi

我在Angular js中放了两个模型

// full device list
$scope.devices = [{id:1, on:true}, {id:2, on:false}, {id:3, on:true}];
// devices with on == true
$scope.devicesOn = $scope.devices.filter(function(t){return t.on==true});
第页:

<span>the following {{ devicesOn.length }} of {{ devices.length }} are on</span>
<ul>
  <li ng-repeat='device in devicesOn'>{{device.id}}</li>
</ul>
下列{{devices.length}}中的{{devices.length}}处于启用状态
  • {{device.id}
因此,设备的模型是动态的。但是,
devicesOn
不是动态的,因为它不是惰性计算

我知道您可以编写类似于
$scope.countOnDevices=function(){return…}
的东西,但这并不理想。有没有更优雅的方法来解决这个问题


这就像从RDBMS中现有的
表创建
视图
(与
where
过滤器一起应用)。

您可以使用观察者观察设备列表的变化,当列表发生任何变化时,可以触发逻辑

$scope.$watch('devices', function () {
    $scope.devicesOn = $scope.devices.filter(function (t) {
        return t.on == true
    });
}, true)

@sza的答案非常有效,我还从这里找到了另一个解决方案:

然后简单地使用
{{filtered.length}


演示

感谢您的快速回答,我忘了提到
devicesOn
需要显示在
  • 列表中。不管怎样,基本上我每次都需要观看
    设备
    模型并创建一个新的
    设备
    ng-repeat='item in filtered = (items | filter:filterExpr)