简体   繁体   中英

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

Fairly new to angular and I am still learning:

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

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:

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;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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