我有一个 angular.js 表(使用智能表模块/插件),我正在尝试根据窗口中设置的一些数据来更新它,特别是数据的设置方式。
window.checker={};window.checker.checked = [{'ip':9,'port':0}]
然后我尝试使用下面的代码将该数据加载到我的 angular.js 表中
angular.module('myApp', ['smart-table'])
.controller('mainCtrl', ['$scope', function ($scope) {
setInterval(x($scope), 1000)
function x($scope){
function createRandomItem(ip, port) {
var
firstName = ip,
lastName = port,
age = Math.floor(Date.now() / 1000)
return{
firstName: firstName,
lastName: lastName,
age: age
};
}
$scope.displayed = [];
if (window.checker && window.checker.checked) {
for (var j = 0; j < window.checker.checked.length; j++) {
$scope.displayed.push(createRandomItem(window.checker.checked[j].ip, window.checker.checked[j].port));
}
}
}
}])
.directive('stRatio',function(){
return {
link:function(scope, element, attr){
var ratio=+(attr.stRatio);
element.css('width',ratio+'%');
}
};
});
理论上,它应该自动抓取 window.checker.checked 并填写结果,但在没有发生的情况下,我目前对 Angular 的了解并不能帮助我找到任何解决方案。
最佳答案
我发现该行存在潜在问题
setInterval(x($scope), 1000)
setInterval
期望获得对函数的引用,但在这里您直接调用它。您可以通过将 x
定义放入匿名函数中或直接在 setInterval
x
定义为函数回调来修复此问题
关于javascript - angular.js 更新变量的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937333/