Doesn't matter when I put it in the app.js file it keeps getting undefined..
app.js
(function () {
var tkcApp = angular.module('TKC', []);
var shuffle = function (o) {
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i),
x = o[--i], o[i] = o[j], o[j] = x);
};
tkcApp.controller('ShuffleCtrl', function ($scope) {
var quotes = [
"Whatever the mind of man can conceive and believe, it can achieve. –Napoleon Hill",
"You miss 100% of the shots you don’t take. –Wayne Gretzky",
];
$scope.getQuote = function () {
$scope.array = shuffle(quotes).slice(0, 1);
};
$scope.getQuote();
})
})();
view
<div class="col-md-6" ng-controller="ShuffleCtrl" onload="getQuote()">
<p ng-repeat="value in array">{{ value }}</p>
</div>
I've put outside the controller as the example I got this from showed, and tried using it as $scope.shuffle, but I just doesn't work. And yes I have body ng-app="TKC"
The problem is that shuffle
function you use in your code operates 'in-place' - it affects the array sent in it as its param, returning nothing. Yet you attempt to use its result immediately.
So either add return o
into shuffle
code, or change the using code like that:
shuffle(quotes);
$scope.array = quotes.slice(0, 1);
i found out the answer from a co-worker. $scope.array gets empty since shuffle doesn't return a value, so putting 'return o' at the bottom fixed the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.