我怎樣才能使TestService的工廠從POST請求返回的結果?還是使應用程序,工廠更新overallcontroller
內的一些細節$scope
多數民衆贊成?
我怎樣才能更新overallcontroller
從另一個控制器內的信息?
app.factory('testservice', ['$http', 'auth', function($http, auth) {
var o = {
posts : []
};
o.test = function() {
return $http.post('/poster', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
console.log(Data);
});
};
return o;
}]);
app.controller('overallController', ['$scope', 'posts', 'testservice', 'auth','$interval',
function($scope, posts, testservice, auth, $interval) {
$scope.data = {cash:"12879999",location:"test2",gang:"None","username":"test",
xp: 1290,
health: 100,
wanted: 30,
energy: 90};
var databackground = function() {
console.log("logging...");
var t = testservice.test;
console.log(t);
}
databackground();
$interval(databackground, 30000);
}]);
例如HTML
<div class="main" ng-controller="overallController">
<section class="sides left" style="background:blue; height:100px;">
<ul>
<li ng-hide="isLoggedIn()"><a href="/#/login">Logg inn</a></li>
</ul>
</section>
<div ng-controller"othercontroller">
// call made from some code here
</div>
</div>
如果在MainCtrl控制器中調用它,並且overallcontroller持有$ scope.data info,那麼在mainctrl中調用它時如何將結果傳輸到overallcontroller中? – maria
如果整體控制器需要這些數據,那麼爲什麼要將該代碼放在MainCtrl中,而不是放在overallcontroller中? –
,因爲我覺得我應該在mainctrl範圍內完成一些特殊的輸入後調用更新後的數據。在頁面上使用一個控制器而不是兩個更好? – maria