Angularjs 没有超时服务的Angular指令不工作
在angularJS中,我编写了一个显示数据的指令。此数据由http服务从远程数据库获取 若我在代码中应用了下面的超时服务,那个么当http服务在一秒钟内返回时,结果将显示在html页面上Angularjs 没有超时服务的Angular指令不工作,angularjs,Angularjs,在angularJS中,我编写了一个显示数据的指令。此数据由http服务从远程数据库获取 若我在代码中应用了下面的超时服务,那个么当http服务在一秒钟内返回时,结果将显示在html页面上 $timeout(function () { $scope.treeRoot = $scope.$eval('serviceResult') || {}; }, 1000); 若我并没有使用超时服务,那个么页面不会显示任何数据,因为$scope.treeRoot是空的,例如 $scope
$timeout(function () {
$scope.treeRoot = $scope.$eval('serviceResult') || {};
}, 1000);
若我并没有使用超时服务,那个么页面不会显示任何数据,因为$scope.treeRoot是空的,例如
$scope.treeRoot = $scope.$eval('serviceResult') || {};
在生产中,http服务可能需要一秒钟以上的时间才能返回,是否有任何通用解决方案。您应该使用Angular或允许返回承诺的服务
解决承诺(例如$http请求)后,可以使用结果填充$scope.treeRoot
如果有帮助,请告诉我。您应该使用Angular's或允许返回承诺的服务
解决承诺(例如$http请求)后,可以使用结果填充$scope.treeRoot
如果有帮助,请告诉我。正如一个答案中建议的那样,您必须使用$http承诺并在响应中捕获结果 下面是一个带有一些代码的示例 服务:
app.factory('myService', ['$http',
function($http){
var obj = {};
obj.get = function (q) {
return $http.get('/your_get_url')
};
obj.post = function (q, object) {
return $http.post('/your_post_url', object)
};
return obj;
}
]);
在控制器中
allControllers.controller('changePasswordController', ['$scope','myService',
function($scope,myService) {
myService.get().then(function (result) {
$scope.treeRoot = 'use your result here'
},function error(result)
{
console.log(result)
})
}
])
这将取代超时问题。正如一个答案中所建议的,您必须使用$http承诺并在响应中捕获结果 下面是一个带有一些代码的示例 服务:
app.factory('myService', ['$http',
function($http){
var obj = {};
obj.get = function (q) {
return $http.get('/your_get_url')
};
obj.post = function (q, object) {
return $http.post('/your_post_url', object)
};
return obj;
}
]);
在控制器中
allControllers.controller('changePasswordController', ['$scope','myService',
function($scope,myService) {
myService.get().then(function (result) {
$scope.treeRoot = 'use your result here'
},function error(result)
{
console.log(result)
})
}
])
这将取代超时问题。您能显示指令的HTML和链接功能吗?使用
$scope.$eval
似乎是个奇怪的地方。如果显示整个函数,而不仅仅是$timeout
,那么它可能更有意义。能否显示指令的HTML和链接函数?这似乎是使用$scope.$eval
的奇怪地方。如果显示整个函数,而不仅仅是$timeout
,作为示例,它可能更有意义。