2016-03-23 12 views
1

AngularJSの学習に関する本のコード例を説明していますが、本の説明どおりに動作しないものがあります。 ChromeとFirefoxの両方でhtmlページを開くと、最後のものまで角度式に問題はありません。その最後の口ひげを得て、正しいものを見せるためのアイデアは何ですか?AngularJSとJS Math.max.apply()が連携していません

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

あなたは直接interpolation' 'から関数を呼び出す行い、' $ scope.Math = Math'内のコントローラヘクタールない限り、スコープに 'Math'オブジェクトのメソッドにアクセスすることはできません{{}}、そして 'Math。* 'の計算はコントローラ側で行います。 '{{calculate {removedArr}}}' –

+0

のように{{Math.max.apply(...)}}を{{Math.max(1,2,3)}}に変更するとうまくいきます。だから私はあなたがこの状況に当てはまるとは言い切れない。 – waterprincess

+0

ビューにその種のロジックを入れないでください...コントローラに属しています – charlietfl

答えて

0

それは働いていなかった、そして最善の解決策のために@charlietflする理由は単純な理由を与えるための@ dnc253に感謝します。これは私の初心者のやり方です。

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 
関連する問題