0

I have such routes:

.config(function config($stateProvider) {
  $stateProvider.state('articles', {
    url: '/articles',
    views: {
      main: {
        controller: 'ArticlesCtrl',
        templateUrl: 'actions/articles/articles.html'
      }
    }
  })
  .state('articles.edit', {
    url: '/upd/:itemId',
    views: {
      '': {
        templateUrl: 'actions/articles/articles.edit.html',
        controller: 'ArticlesEditCtrl',
      }
    }
  })
  .state('articles.add', {
    url: '/add/news/',
    views: {
      '': {
        templateUrl: 'actions/articles/articles.add.html',
        controller: 'ArticlesAddCtrl',
      }
    }
  })
})

and i'm updating some data in articles state, how can i force child controller with state articles.edit to update it's $scope.someVar with data from first state controller, without using services? Which way of solving this issue it the best one?

3
  • you need $broadcast check dotnet-tricks.com/Tutorial/angularjs/… Commented Apr 6, 2016 at 6:57
  • Sounds like you need to organize routes so you will have 'articles' as parent route(abstract) and sub routes like edit and add. I this case you will be able to place common logic in "abstract" controller. Commented Apr 6, 2016 at 7:01
  • @singhakash it's a bad idea. Commented Apr 6, 2016 at 9:19

2 Answers 2

0

Since controller functions are 'newed' objects you could use the prototype of controllers to pass data around through an object like this

var obj = {}
function ArticlesCtrl($scope){
  obj.ArticlesCtrl = ArticlesCtrl.prototype

  ArticlesCtrl.prototype.passData = function(data){
    //...do stuff with data passed from ArticlesEditCtrl
  }
  
}

function ArticlesEditCtrl(){
  obj.ArticlesCtrl.passData('some data here')
}

.config(function config($stateProvider) {
  $stateProvider.state('articles', {
    url: '/articles',
    views: {
      main: {
        controller: 'ArticlesCtrl',
        templateUrl: 'actions/articles/articles.html'
      }
    }
  })
  .state('articles.edit', {
    url: '/upd/:itemId',
    views: {
      '': {
        templateUrl: 'actions/articles/articles.edit.html',
        controller: 'ArticlesEditCtrl',
      }
    }
  })
  .state('articles.add', {
    url: '/add/news/',
    views: {
      '': {
        templateUrl: 'actions/articles/articles.add.html',
        controller: 'ArticlesAddCtrl',
      }
    }
  })
})

Sign up to request clarification or add additional context in comments.

Comments

0

put your function resolve just like that

$stateProvider.state('articles', {
url: '/articles',
views: {
  main: {
    controller: 'ArticlesCtrl',
    templateUrl: 'actions/articles/articles.html'
  }resolve: {
                time: function AnyFunction(SessionTimer)
                {
                    SessionTimer.startTimer();
                }
            }

1 Comment

could you provide an example with variables?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.