简体   繁体   English

角度-如果选中此复选框,则更改值

[英]Angular - If checkbox is checked change value

Controller: 控制器:

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

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

});

View: 视图:

<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" />

What I am trying to do is to change <td>{{data.Virtual.SumCores}}</td> to <td>{{data.Virtual.SumCores/4}}</td> if over subscription is equal to true 我想做的是,如果超额订购等于<td>{{data.Virtual.SumCores}}</td><td>{{data.Virtual.SumCores/4}}</td>真正

Fairly new to angular and I am still learning: Angular还很陌生,我仍在学习:

Do I do this in the view or the controller? 我要在视图或控制器中执行此操作吗?

What is the best approach? 最好的方法是什么?

You can do it in the view - just be sure to give your checkbox a ngModel 您可以在视图中进行操作-只需确保为复选框提供ngModel

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

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

Or, with an ngChange on the checkbox and some controller logic: 或者,使用复选框上的ngChange和一些控制器逻辑:

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

Controller: 控制器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM