1

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?

2
  • does the oversubscription apply to the entire repeat or only specific elements? Commented Sep 23, 2015 at 14:02
  • Instead of changing the databinding, I would bind the td's to a new set of variables that get recalculated on the ng-change of the checkbox. Commented Sep 23, 2015 at 14:02

1 Answer 1

3

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;
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

@CMS -- Controller method is probably more efficient - the view method will get processed each time the $digest cycle is executed (depending on your app - could be a lot).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.