Athit Upakan Athit Upakan - 1 month ago 17
AngularJS Question

How use 2 controllers in one input

Need get data from ControllerB and use selected in ControllerA

<select class="form-control selectpicker" title="All" selectpicker
ng-model="ControllerA.Criteria.Status"
ng-options="item as item.DISPLAY for item in ControllerB.ComboStatusListResult">
</select>


Thanks.

Answer

I think the best possible solution is to use data transfer services in angular js to get the ComboStatusListResult in ControllerB to ControllerA and there after using the options from ControllerA itself.

Sample data transfer service.

myApp.factory('DataTransfer', function () {

    var data = {
        Options: ''
    };

    return {
        getOptions: function () {
            return data.Options;
        },
        setOptions: function (Options) {
            data.Options= Options;
        }
    };
});

and from ControllerB set the ComboStatusListResult object to the service.

myApp.controller('ControllerB', function ($scope, DataTransfer) {

    DataTransfer.setOptions($scope.ComboStatusListResult);

});

and in ControllerA receive the options to its scope object

myApp.controller('ControllerA', function ($scope, DataTransfer) {

    $cope.ComboStatusListResult = DataTransfer.getOptions();

});

and in your template use it as.

<select class="form-control selectpicker" title="All" selectpicker
        ng-model="Criteria.Status"
        ng-options="item as item.DISPLAY for item in ComboStatusListResult"></select>
Comments