我正在尝试创建一个包含随机数字的表格,如下所示:
<table>
<tr>
<td>123</td>
<td>315</td>
<td>356</td>
<td>441</td>
</tr>
<tr>
<td>664</td>
<td>252</td>
<td>236</td>
<td>742</td>
</tr>
...
<!-- more rows like the above -->
</table>
我的Html是:
<table>
<tr ng-repeat="item in items">
<td>{{randNumGen()}}</td>
<td>{{randNumGen()}}</td>
<td>{{randNumGen()}}</td>
</tr>
</table>
我的JS是:
app.controller('TableCtrl', function($scope) {
$scope.items = [1,2,3,4,5,6,7,8,9,0];
$scope.randNumGen = function() {
return Math.floor((Math.rand()*1000)+1);
}
});
它返回所有随机数,就像我想要的那样,但它抛出一个JS错误说:
“错误:[$ rootScope:infdig]”
并且不会在此控制器下运行任何脚本。
答案 0 :(得分:6)
randomNumGen()
的结果是,在角度定义中是一个范围变量。如果它检测到对此函数结果的更改,则会触发摘要。由于结果几乎在所有情况下都不同,这将导致无限的摘要循环。
最好在摘要循环之前构建随机数组,然后将静态不变数组分配给范围,从而不会导致任何不需要/不需要的摘要循环。
一种方法是在加载控制器时构建一个简单的工厂来为您生成数组。然后输出它的内容。
angular.module('myapp', [])
.factory('generator', function() {
function buildRandArray(length) {
var arr = [];
for(var i = 0; i < (length || 10); i++) {
var fill = [];
for (var j = 0; j < 3; j++) {
fill.push(Math.floor((Math.random()*1000)+1));
}
arr.push(fill);
}
return arr;
}
return {
buildRandArray : buildRandArray
}
})
.controller('TableCtrl', function($scope, generator) {
$scope.items = generator.buildRandArray(10);
});
现在你的HTML将是
<table>
<tr ng-repeat="item in items">
<td ng-repeat="number in item">
{{number}}
</td>
</tr>
</table>
答案 1 :(得分:0)
检查出来
import pandas as pd
import re
df = pd.DataFrame(["BULL ESTOX 11X S", "BULL ESTOX X12 S"], columns=["name"])
description_map = {"ESTOX":"Euro STOXX 50"}
underlying_match = re.compile(r"\s(\S+)\s")
leverage_match = re.compile(r"\d+X|X\d+", flags=re.IGNORECASE)
def f(value):
f1 = lambda x: description_map[underlying_match.findall(x)[0]] if underlying_match.findall(x)[0] in description_map else ""
f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else ""
f3 = lambda x: "-" if "BEAR" in x else "-" if "SHORT" in x else ""
underlying = f1(value)
leverage = f2(value)
sign = f3(value)
statement = "Tracks " + underlying
if underlying == "":
if sign == "-" and leverage == "":
return statement + "{}".format("inversely.")
if sign == "-" and leverage != "":
return statement + "{} with {}{} leverage.".format("inversely", sign, leverage)
if sign == "" and leverage != "":
return statement + "with {}{} leverage.".format(sign, leverage)
else:
return "Tracks"
if leverage[0].replace("X","x") == "x":
leverage = ''.join(leverage[1:])+leverage[0].replace('X','x')
if leverage != "" and sign == "-":
statement += " {} with {}{} leverage.".format("inversely", sign, leverage)
elif leverage != "" and sign == "":
statement += " with {} leverage.".format(leverage)
else:
if sign == "-":
statement += " {} ".format("inversely")
return statement
df["description"] = df["name"].map(lambda x:f(x))
print df
我注意到Math.rand不是你的代码中的函数!!使用Math.random
答案 2 :(得分:0)
不应该是Math.random()
而不是Math.rand()
吗?见reference on developer.mozilla.org