maria maria - 1 year ago 68
AngularJS Question

update $scope info from another controller

How can i make the testservice factory return the result from the post request?, or make the app, factory update some

details thats within the

how can i update information within the
from another controller?

app.factory('testservice', ['$http', 'auth', function($http, auth) {

var o = {
posts : []

o.test = function() {
return $'/poster', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}


return o;


app.controller('overallController', ['$scope', 'posts', 'testservice', 'auth','$interval',
function($scope, posts, testservice, auth, $interval) {

$ = {cash:"12879999",location:"test2",gang:"None","username":"test",
xp: 1290,
health: 100,
wanted: 30,
energy: 90};

var databackground = function() {
var t = testservice.test;


$interval(databackground, 30000);


example html

<div class="main" ng-controller="overallController">
<section class="sides left" style="background:blue; height:100px;">
<li ng-hide="isLoggedIn()"><a href="/#/login">Logg inn</a></li>

<div ng-controller"othercontroller">
// call made from some code here

Answer Source

Change your service to

o.test = function() {
  return $'/poster', null, {
    headers: {Authorization: 'Bearer '+auth.getToken()}

And in your controller, do call the service, and get the results back in the promise:

testservice.test().then(function(data) {
  $ = data;

Read more about how to use promises here