Angularjs实现数组随机排序的方法
程序员文章站
2024-02-04 19:44:16
如下所示:
如下所示:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>实现数组随机排序</title> //需要导入angular.js库文件 <script type="text/javascript" src="../angular-1.5.5/angular-1.5.5/angular.js"></script> <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}}<br><button ng-click="mysort2()">点击随机排序</button> <br>{{arr}} <!--{{arr}}<button ng-click="mysort2()">点击随机排序</button> {{newarr}}--> </body> </html>
以上这篇angularjs实现数组随机排序的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇: 批量引用iconfont字体图标到项目