2016-03-23 3 views
1

Я просматриваю пример кода в книге об обучении AngularJS, и что-то не работает, как показывает книга. Открытие html-страницы в Chrome и Firefox не вызывает проблем с угловыми выражениями до последнего. Любые идеи, чтобы получить последние усы, чтобы показать правильную вещь?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

Вы не можете непосредственно получить доступ к 'метод объекта Math' по размаху, если контроллер га в' $ scope.Math = Math', зовите функцию от 'interpolation' {{}}, и вычисление 'Math. *' выполняется со стороны контроллера. например '{{calculate (removedArr)}}' –

+0

Если я изменил {{Math.max.apply (...)}} на {{Math.max (1,2,3)}}, он отлично работает. Поэтому я не уверен, что вы сказали в этой ситуации. – waterprincess

+0

Не ставьте эту логику в вид ... она принадлежит вашему контроллеру – charlietfl

ответ

0

Спасибо @ dnc253 для придания простой причине, почему он не работает, и @charlietfl лучшее решение. Вот мой новичок, способный сделать эту работу.

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 
Смежные вопросы