Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a hairy one to answer, because you didn't give the full context of what you're doing. The accepted answer will work, but in some cases will cause poor performance. That, and it's going to be harder to test.</p> <p>If you're doing this as part of a static form, fine. The accepted answer will work, even if it isn't easy to test, and it's hinky.</p> <h3>If you want to be "Angular" about this:</h3> <p>You'll want to keep any "business logic" (i.e. logic that alters data to be displayed) out of your views. This is so you can unit test your logic, and so you don't end up tightly coupling your controller and your view. Theoretically, you should be able to point your controller at another view and use the same values from the scopes. (if that makes sense).</p> <p>You'll also want to consider that any function calls inside of a binding (such as <code>{{}}</code> or <code>ng-bind</code> or <code>ng-bind-html</code>) will have to be evaluated on <em>every digest</em>, because angular has no way of knowing if the value has changed or not like it would with a property on the scope.</p> <p>The "angular" way to do this would be to cache the value in a property on the scope on change using an ng-change event or even a $watch.</p> <p>For example with a static form:</p> <pre class="lang-js prettyprint-override"><code>angular.controller('MainCtrl', function($scope, $window) { $scope.count = 0; $scope.total = 1; $scope.updatePercentage = function () { $scope.percentage = $window.Math.round((100 * $scope.count) / $scope.total); }; }); </code></pre> <pre class="lang-html prettyprint-override"><code>&lt;form name="calcForm"&gt; &lt;label&gt;Count &lt;input name="count" ng-model="count" ng-change="updatePercentage()" type="number" min="0" required/&gt;&lt;/label&gt;&lt;br/&gt; &lt;label&gt;Total &lt;input name="total" ng-model="total" ng-change="updatePercentage()" type="number" min="1" required/&gt;&lt;/label&gt;&lt;br/&gt; &lt;hr/&gt; Percentage: {{percentage}} &lt;/form&gt; </code></pre> <h3>And now you can test it!</h3> <pre><code>describe('Testing percentage controller', function() { var $scope = null; var ctrl = null; //you need to indicate your module in a test beforeEach(module('plunker')); beforeEach(inject(function($rootScope, $controller) { $scope = $rootScope.$new(); ctrl = $controller('MainCtrl', { $scope: $scope }); })); it('should calculate percentages properly', function() { $scope.count = 1; $scope.total = 1; $scope.updatePercentage(); expect($scope.percentage).toEqual(100); $scope.count = 1; $scope.total = 2; $scope.updatePercentage(); expect($scope.percentage).toEqual(50); $scope.count = 497; $scope.total = 10000; $scope.updatePercentage(); expect($scope.percentage).toEqual(5); //4.97% rounded up. $scope.count = 231; $scope.total = 10000; $scope.updatePercentage(); expect($scope.percentage).toEqual(2); //2.31% rounded down. }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload