-4

I have a controller name is ParentController, which has a dropdown named ddlState. How to access this dropdown in another controller named CityController in Angular JS

  • 1
    Can you show us some code? So we can start to discuss based on what you tried. – Andrea Nov 22 '16 at 09:22
  • We have lots of code related this one so please search then post your question. I think its duplicate http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js – Vijay Maheriya Nov 22 '16 at 09:23

2 Answers2

0

use a factory.

Here is a sample for simple data exchange

app.factory('dataExchange', function() {
    var ret = {
        getData: getData,
        setData: setData
    };

    var data = {}; 

    return ret;

    function getData() {
        return data;
    }

    function setData(x) {
        data = x;
    }
});

Simply give your object to setData in your parent controller, and get it back in your child with getData

0

You have different ways to work with this, 1. It is always better to have the drop downs through a directive.then you can use the same directive for multiple drop down in multiple controllers by using $on function. 2. Else if you want to write and repeat the drop down code in all html and controllers, you can go with local storage system, So once call the API result and store in local storage (browser level), in other controllers also with out calling the API you can append the data into drop down from local storage.

Other way is keep the drop down values in a $rootScope variable,and you can use in where ever you want. (but this way is not recommended)

Kamal Gadepalli
  • 177
  • 1
  • 1
  • 11