gpt4 book ai didi

javascript - 获取错误 : shuffle(. ..) 在 angularJS 中未定义

转载 作者:行者123 更新时间:2023-12-02 17:01:17 25 4
gpt4 key购买 nike

当我将它放入 app.js 文件中时,它一直未定义,这并不重要。

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();
})
})();

查看

<div class="col-md-6" ng-controller="ShuffleCtrl" onload="getQuote()">
<p ng-repeat="value in array">{{ value }}</p>
</div>

我已经将 Controller 放在 Controller 之外,就像我从中得到的示例所示,并尝试将其用作 $scope.shuffle,但我就是不起作用。是的,我有 body ng-app="TKC"

最佳答案

问题是您在代码中使用的 shuffle 函数“就地”运行 - 它会影响作为参数发送的数组,不返回任何内容。然而您试图立即使用它的结果。

因此,要么将 return o 添加到 shuffle 代码中,要么更改如下的 using 代码:

shuffle(quotes);
$scope.array = quotes.slice(0, 1);

关于javascript - 获取错误 : shuffle(. ..) 在 angularJS 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25685850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com