在控制器中,我想在兩個函數之間共享數據。例如:angularjs在函數之間共享變量
controllers.controller('rightDrawerCtrl', function ($scope, $http) {
$scope.shared_id;
$scope.getId = function() {
// get the id via $http, using a secondary id to retrieve the shared_id
}
$scope.useId = function() {
// use the shared_id to make a different api call
}
});
但是在本例中,當調用useId()時,shared_id是未定義的。
我認爲這是因爲useId是'創建',而shared_id是未定義的,所以沒有被通知新的值。
我發現,通過使用兩個控制器來解決這個問題的唯一途徑:Share data between AngularJS controllers
有沒有更好的方式來做到這一點,只使用1個控制器?
如果您必須使用此方法,請在shared_id周圍添加一個超時以首先啓動它。 –