2016-03-23 9 views
1

Ich gehe durch ein Codebeispiel in einem Buch über das Lernen von AngularJS, und etwas funktioniert nicht, wie das Buch zeigt. Das Öffnen der HTML-Seite in Chrome und Firefox hat mit den eckigen Ausdrücken bis zur letzten keine Probleme. Irgendwelche Ideen, um diesen letzten Schnurrbart zu bekommen, um das Richtige zu zeigen ??AngularJS und JS Math.max.apply() funktioniert nicht

angular.module('myApp', []) 
 
    .controller('myController', function($scope) { 
 
    $scope.Math = window.Math; 
 
    $scope.myArr = []; 
 
    $scope.removedArr = []; 
 
    });
<!doctype html> 
 
<html ng-app="myApp"> 
 
    <head> 
 
    <title>AngularJS Expressions</title> 
 
    <style> 
 
     a{color: blue; text-decoration: underline; cursor: pointer} 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <div ng-controller="myController"> 
 
     <h1>Expressions</h1> 
 
     Array:<br> 
 
     {{myArr}}<hr> 
 
     Elements removed from array:<br> 
 
     {{removedArr}}<hr> 
 
     <a ng-click="myArr.push(Math.floor(Math.random()*100 + 1))"> 
 
     Click to append a value to the array</a><hr> 
 
     <a ng-click="removedArr.push(myArr.shift())"> 
 
     Click to remove the first value from the array</a><hr> 
 
     Size of Array:<br> 
 
     {{myArr.length}}<hr> 
 
     Max number removed from the array:<br> 
 
     {{Math.max.apply(Math, removedArr)}}<hr> 
 
\t </div> 
 
    <script src="http://code.angularjs.org/1.3.0/angular.min.js"></script> 
 
    <script src="js/expressions_javascript.js"></script> 
 
    </body> 
 
</html>

+0

Sie können nicht direkt zugreifen 'Math' Objektmethode auf Umfang, es sei denn, Controller ha in' $ scope.Math = Math', rufen eine Funktion von 'interpolation' {{}} und die Berechnung 'Math. *' auf der Controllerseite. wie '{{calculate (removedArr)}}' –

+0

Wenn ich {{Math.max.apply (...)}} zu {{Math.max (1,2,3)}} ändere, funktioniert es gut. Ich bin mir also nicht sicher, was Sie in dieser Situation gesagt haben. – waterprincess

+0

Setzen Sie diese Art von Logik nicht in die Ansicht ... es gehört in Ihren Controller – charlietfl

Antwort

0

Vielen Dank an @ dnc253 aus dem einfachen Grund geben, warum es nicht funktioniert, und für die beste Lösung, um @charlietfl. Hier ist meine neue Art, diese Arbeit zu machen.

angular.module('myApp', []) 
 
    .controller('myController', function($scope) { 
 
    $scope.Math = window.Math; 
 
    $scope.myArr = []; 
 
    $scope.removedArr = []; 
 
    $scope.maxNumm; 
 
    
 
    $scope.maxNummm = function() 
 
    { 
 
    \t $scope.maxNumm = Math.max.apply(Math, $scope.removedArr); 
 
    \t console.log("maxNumm: " + $scope.maxNumm); 
 
    }; 
 
    
 
    });
<!doctype html> 
 
<html ng-app="myApp"> 
 
    <head> 
 
    <title>AngularJS Expressions</title> 
 
    <style> 
 
     a{color: blue; text-decoration: underline; cursor: pointer} 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <div ng-controller="myController"> 
 
     <h1>Expressions</h1> 
 
     Array:<br> 
 
     {{myArr}}<hr> 
 
     Elements removed from array:<br> 
 
     {{removedArr}}<hr> 
 
     <a ng-click="myArr.push(Math.floor(Math.random()*100 + 1))"> 
 
     Click to append a value to the array</a><hr> 
 
     <a ng-click="removedArr.push(myArr.shift()); maxNummm();"> 
 
     Click to remove the first value from the array</a><hr> 
 
     Size of Array:<br> 
 
     {{myArr.length}}<hr> 
 
     Max number removed from the array:<br> 
 
     <!-- For security reasons, you can no longer invoke the 
 
     \t apply function in an angular expression --> 
 
     <!-- {{Math.max.apply(Math, removedArr)}} --> 
 
     {{maxNumm}} 
 
     <hr> 
 
\t </div> 
 
    <script src="http://code.angularjs.org/1.3.0/angular.min.js"></script> 
 
    <script src="js/expressions_javascript.js"></script> 
 
    </body> 
 
</html>

enter code here