<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="../libs/angular.min.js"></script>
<script>
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() {
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;
})
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
{{newArr}}<br><button ng-click="mySort2()">点击随机排序</button> <br>{{arr}}
</body>
</html>