0
我有一個控制器(MyCtrl)。首先要做的是做一個http.get
的調用並獲得響應並將其分配給$scope.input
。控制器的其餘部分取決於$scope.input
。但問題是控制器中的代碼嘗試在http調用完成之前訪問$scope.input
。
我該如何解決這個問題?
app.controller('MyCtrl', function($scope, $http, $routeParams, factory) {
factory.getInfo($routeParams.id)
.success(function(response) {
//The factory code make the http.get call
$scope.input = response;
});
//Rest of code accessing $scope.input before it is ready
});
PS:我不想放置rest of controller code
的success
塊內
感謝
你使用角度路由器還是ui路由器? – Arkantos
我正在使用routeProvider – user7
不能將所有的初始化邏輯封裝在函數中,然後在成功回調中調用該函數嗎? – Arkantos