Angular - 如果选中复选框,则更改值

时间:2015-09-23 13:59:06

标签: javascript angularjs

控制器:

promiseObj.physical = $http.get('search?sumBoth=1&customer=' + customer).success(function(data) {

$scope.servers = data; // get data from json

});

查看:

<tr ng-repeat="data in servers | filter: { _id: { idc: item._id.idc } }">
            <td>{{data._id.cluster}}</td>
            <td>{{data.Physical.SumCores}}</td>
            <td>{{data.Virtual.SumCores}}</td>
            <td>{{data.Physical.SumMemory}}</td>
</tr>

Oversubscription? <input type="checkbox" name="oversubscription" />

我想要做的是,如果超额订阅等于真,则将<td>{{data.Virtual.SumCores}}</td>更改为<td>{{data.Virtual.SumCores/4}}</td>

角度相当新,我还在学习:

我是在视图还是控制器中执行此操作?

最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以在视图中执行此操作 - 只需确保将复选框设为ngModel

即可
Oversubscription? <input type="checkbox" ng-model="overSubscription" name="oversubscription" />

<td>{{overSubscription ? (data.Virtual.SumCores / 4) : data.Virtual.SumCores}}</td>

或者,复选框上有一个ngChange和一些控制器逻辑:

Oversubscription? <input type="checkbox" ng-change="calcCores(overSubscription) ng-model="overSubscription" name="oversubscription" />

控制器:

$scope.calcCores = function(checked) {
    $scope.servers = $scope.servers.map(function(server) {
        server.Virtual.SumCores = checked ? (server.Virtual.SumCores / 4) : server.Virtual.SumCores
        return server;
    });
}