AngularJS和JS Math.max.apply()不能一起工作

时间:2016-03-23 21:07:06

标签: javascript angularjs

我正在阅读一本关于学习AngularJS的书中的代码示例,并且正如本书所示,它没有起作用。在Chrome和Firefox中打开html页面,直到最后一个角度表达式都没有问题。任何想法,以获得最后的胡子,以显示正确的东西?



typedef struct {
    int x, y;                   /* location of window */
    int width, height;          /* width and height of window */
    int border_width;           /* border width of window */
    int depth;                  /* depth of window */
    Visual *visual;             /* the associated visual structure */
    Window root;                /* root of screen containing window */
#if defined(__cplusplus) || defined(c_plusplus)
    int c_class;                /* C++ InputOutput, InputOnly*/
#else
    int class;                  /* InputOutput, InputOnly*/
#endif
    int bit_gravity;            /* one of bit gravity values */
    int win_gravity;            /* one of the window gravity values */
    int backing_store;          /* NotUseful, WhenMapped, Always */
    unsigned long backing_planes;/* planes to be preserved if possible */
    unsigned long backing_pixel;/* value to be used when restoring planes */
    Bool save_under;            /* boolean, should bits under be saved? */
    Colormap colormap;          /* color map to be associated with window */
    Bool map_installed;         /* boolean, is color map currently installed*/
    int map_state;              /* IsUnmapped, IsUnviewable, IsViewable */
    long all_event_masks;       /* set of events all people have interest in*/
    long your_event_mask;       /* my event mask */
    long do_not_propagate_mask; /* set of events that should not propagate */
    Bool override_redirect;     /* boolean value for override-redirect */
    Screen *screen;             /* back pointer to correct screen */
} XWindowAttributes;

angular.module('myApp', [])
  .controller('myController', function($scope) {
    $scope.Math = window.Math;
    $scope.myArr = [];
    $scope.removedArr = [];
  });




1 个答案:

答案 0 :(得分:0)

感谢@ dnc253给出了它无法正常工作的简单原因,并感谢@charlietfl获得最佳解决方案。这是我新手的工作方式。

angular.module('myApp', [])
  .controller('myController', function($scope) {
    $scope.Math = window.Math;
    $scope.myArr = [];
    $scope.removedArr = [];
    $scope.maxNumm;
    
    $scope.maxNummm = function()
    {
    	$scope.maxNumm = Math.max.apply(Math, $scope.removedArr);
    	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
      	apply function in an angular expression -->
        <!-- {{Math.max.apply(Math, removedArr)}} -->
        {{maxNumm}}
        <hr> 
	</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