2017-05-15 5 views
0

コントローラに問題があり、Gui $scopeが更新されません。 調査の結果、$timeoutを使用できることが判明しました。

サービスが異なるユニットで異なる時間がかかるため、アプリケーションの速度が遅いために5000タイムアウトに設定したくありません。 これを行うための他の方法はありますか?

.service('RowService', function ($q) { 

var rows = undefined; 
this.getrows = function (id) { 
    var local = new PouchDB('db'); 
    var deferred = $q.defer(); 
    //  var rows = []; 
    local.query(function (doc, emit) { 
    emit(doc.type); 
    }, { key: 'row_detail' }).then(function (result) { 
    var rows = []; 
    for (i = 0; i < result.rows.length; i++) { 
     local.get(result.rows[i].id).then(function (response) { 
     if (response.orderid == orderId) { 
      rows.push(response); 
     } 
     deferred.resolve(rows); 
     }) 
    } 
    }).catch(function (err) { 
    // handle any errors 
    }); 

    rows = deferred.promise; 

    return $q.when(rows); 
} 

}) 


RowService.getrows($stateParams.id).then(function (data) { 

    // Not working example 
    $scope.rows = data; 
    // Working example 

    $timeout(function() { 
    $scope.rows = data; 
    }, 5000) 

} 
+0

'$ timeout'関数は、タイムアウトの終わりに' $ scope。$ apply() 'を呼び出します。したがって、' $ scope.rows 'を指定した後に '$ scope。$ apply()'を呼び出すことができます。 =データ '? –

+0

なぜあなたはdeferred.resolve(rows)を持っていますか?あなたのforループの中に? –

答えて

0

$スコープ$(適用)時には、問題につながることができますが、$タイムアウトが遅滞なく呼び出すことができます。

.service('RowService', function ($q) { 

var rows = undefined; 
this.getrows = function (id) { 
    var local = new PouchDB('db'); 
    var deferred = $q.defer(); 
    //  var rows = []; 
    local.query(function (doc, emit) { 
    emit(doc.type); 
    }, { key: 'row_detail' }).then(function (result) { 
    var rows = []; 
    for (i = 0; i < result.rows.length; i++) { 
     local.get(result.rows[i].id).then(function (response) { 
     if (response.orderid == orderId) { 
      rows.push(response); 
     } 
     deferred.resolve(rows); 
     }) 
    } 
    }).catch(function (err) { 
    // handle any errors 
    }); 

    rows = deferred.promise; 

    return $q.when(rows); 
} 

}) 


RowService.getrows($stateParams.id).then(function (data) { 

    // Not working example 
    $scope.rows = data; 
    // Working example 

    $timeout(function() { 
    $scope.rows = data; 
    }) 

} 
0

はその後、それが動作するタイムアウト値および機能の意志を提供していません。すべての関数が実行された後に呼び出されます。さらに、あなたのサービスで約束しているので、タイムアウト値は必要ありません。

関連する問題