gpt4 book ai didi

javascript - 调用函数来更新一些变量

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

我有一个从ajax调用中获得的集合lines,我使用ng-repeat,并且对于每个项目line属性 line.date 需要进行一些修改才能显示

问题是我不知道如何调用函数来进行修改?

我尝试 data-ng-initng-init 函数被调用,但变量未更新!

HTML代码

 <div ng-controller="lineController" data-ng-init="loadLines()">
<div ng-repeat="line in lines">
...
<div data-ng-init="loadDates(line.date)">
...
{{ leftDays}}
</div>
</div>
</div>

JS代码:

var app = angular.module("searchModule", []);

app.controller("lineController", function ($scope, $http)
{
// LOAD LINES AJAX
$scope.loadLines = function () {

$http({method: 'GET', url: '..'}).success(function(data) {

angular.forEach(data.content, function(item, i) {
$scope.lines.push(item);
});

});

};

$scope.loadDates = function (date) {
// complex updating of date variable
....
$scope.leftDays = ...;
};

});

最佳答案

为什么不管理 angular.forEach 中的每行?像这样:

$http({method: 'GET', url: '..'}).success(function(data) {

angular.forEach(data.content, function(item, i) {
//Do stuff to item here before pushing to $scope.lines
//item.date = new Date(item.date) blah blah
$scope.lines.push(item);
});
});

如果您希望 line.date 在 html 中以不同的方式显示,并且不想修改实际数据,为什么不使用 $filter 呢?像这样:

<span ng-repeat="line in lines">{{line.date|yourCustomFilter}}</span>

关于javascript - 调用函数来更新一些变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38326151/

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