美文网首页
Android之父_实现数组随机排序(AngularJS)

Android之父_实现数组随机排序(AngularJS)

作者: _帅的掉渣 | 来源:发表于2017-09-19 10:52 被阅读0次
    <script type="text/javascript">
                var app = angular.module("myApp", []);
                var arr1 = [1, 2, 3, 7, 4, 9, 5, 6];
                app.service("sortService", function() {
                    this.arr = [1, 2, 3, 7, 4, 9, 5, 6];
                    this.t;
                    this.mySort = function() {
                        //alert("haha");
                        for(var i = 0; i < this.arr.length; i++) {
                            var rand = parseInt(Math.random() * this.arr.length);
                            this.t = this.arr[rand];
                            this.arr[rand] = this.arr[i];
                            this.arr[i] = this.t;
                        }
                    }
                })
                app.controller("myCtrl", function($scope, sortService) {
                    $scope.arr = arr1;
                    $scope.newArr = sortService.arr;
                    $scope.mySort2 = sortService.mySort;
                    
                    /*$scope.mySort2 = function(){
                        sortService.mySort();
                    }*/
                })
            </script>
        </head>
    
        <body ng-app="myApp" ng-controller="myCtrl">
            {{newArr}}<button ng-click="mySort2()">点击随机排序</button> {{arr}}
            <!--{{arr}}<button ng-click="mySort2()">点击随机排序</button> {{newArr}}-->
        </body>
    
    

    相关文章

      网友评论

          本文标题:Android之父_实现数组随机排序(AngularJS)

          本文链接:https://www.haomeiwen.com/subject/sakwsxtx.html