在promise success函数中赋值后,AngularJS值为NaN
在过去的几个小时里,我一直在努力解决这个问题,我不知道该怎么办。我正在做一个AngularJS应用程序,我使用AngularTimer指令。 当我在ng repeat中将TimerValue值动态分配给end time属性时,它工作得非常好。但当我在使用routeParams的详细信息页面中使用它时,它失败了在promise success函数中赋值后,AngularJS值为NaN,angularjs,variable-assignment,nan,angular-promise,Angularjs,Variable Assignment,Nan,Angular Promise,在过去的几个小时里,我一直在努力解决这个问题,我不知道该怎么办。我正在做一个AngularJS应用程序,我使用AngularTimer指令。 当我在ng repeat中将TimerValue值动态分配给end time属性时,它工作得非常好。但当我在使用routeParams的详细信息页面中使用它时,它失败了 <timer end-time="itemDetail.startDate"> 页面控制器 app.controller('detailsController', ['$
<timer end-time="itemDetail.startDate">
页面控制器
app.controller('detailsController', ['$scope', '$routeParams','itemsService',
function($scope, $routeParams,itemsService) {
itemsService.getItemById($routeParams.itemId).then(function(data){
$scope.itemDetail = data;
});
}]);
工厂
app.factory('itemsService', ['$http','$filter', function($http, $filter){
var itemsService = {
getAllItems: function() {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get('app/jsons/items.json').then(function (response) {
return response.data;
});
// Return the promise to the controller
return promise;
}, getItemById : function(id) {
var promise = $http.get('app/jsons/items.json').then(function (response) {
return $filter('filter')(response.data, {id: id})[0];
});
// Return the promise to the controller
return promise;
}
};
return itemsService;
}]);
在我看来,我是这样使用它的
<div class="large-12 columns" >
Remaning time: <timer end-time="startDate">{{days}} Days, {{hours}} Hours, {{minutes}} Minutes, {{seconds}} Seconds.</timer>
</div>
<div ng-repeat="item in itemDetails">
{{item.name}}
Remaning time: <timer end-time="item.StartDate">{{days}} Days, {{hours}} Hours, {{minutes}} Minutes, {{seconds}} Seconds.</timer>
</div>
但如果我这样做就不行了:
<timer end-time="itemDetail.startDate">
为什么会发生这种情况?结束时间属性需要eval表达式
<timer end-time="{{itemDetail.startDate}}">
好的,伙计们。我找到了一个对我有效的解决方案,虽然很简单。问题似乎与angular执行其方法的方式有关。当我将筛选对象分配给$scope.itemDetail=filteredObj时。当我试图在html中使用
itemDetail.startDate
时,该值没有得到正确解析,我看到了NAN。因此,当我连接RESTAPI时,这可能会正常工作,RESTAPI仍在开发中,但目前我使用的是带有虚拟数据的模拟JSON,上帝知道在幕后发生了什么。我使用的解决方案只是使用ng repeat,而不是直接从html访问对象属性。它成功了
解决方案
我向保存我的项目详细信息的父div添加了一个ng repeat指令。只需访问这样的属性
<div class="large-12 columns" >
Remaning time: <timer end-time="startDate">{{days}} Days, {{hours}} Hours, {{minutes}} Minutes, {{seconds}} Seconds.</timer>
</div>
<div ng-repeat="item in itemDetails">
{{item.name}}
Remaning time: <timer end-time="item.StartDate">{{days}} Days, {{hours}} Hours, {{minutes}} Minutes, {{seconds}} Seconds.</timer>
</div>
{{item.name}
还押时间:{天}天、{小时}小时、{分钟}分钟、{秒}秒。
在这种情况下,它的工作方式与我的itemDetails对象数组中的任何一种方式相同,我只有一个对象,因为我根据从$routeParams传递的ID过滤数据检查itemDetail.startDate是否为string not Num这也没什么帮助:|这是出现Nan问题时我做的第一件事,但是。在这种情况下,整个页面正在断开
<timer end-time="itemDetail.startDate">