1

I'm having a problem sharing data between controllers, I used a service, seems working but something doesn't work as it should.

My problem is: When I click a row (in the ng-repeat part), it fires correctly service function (ui_mgmt), but it doesn't update ui_controller(controller) $scope.show_details var!

JS Code

ui_service_f = function() {
    //Service which should update Controller 1 var
    this.show_details = "-1";
    this.ui_mgmt = function(det,data) {
        this.show_details = 1;
    };
};
ui_controller =function($scope,ui_service) { 
    //Controller1
    //I think this part is bugged      
    $scope.show_details = ui_service.show_details;
    $scope.sub_bottom_menu = ui_service.sub_bottom_menu;
};
sim_list_placer =function($scope,service_sim,ui_service) {
    //Controller2
    $scope.ui_mgmt = function(a,b) {
        ui_service.ui_mgmt(a,b);
    };
};

HTML

<div ng-controller="sim_list_placer">
    <button ng-click="reload_sim_list()">Reload</button>
    <table class="td_mc_table">
        <thead>
            <td>pk</td><td>ICCID</td><td>Operatore</td>
            <td>Numero Dati</td><td>PIN</td><td>PUK</td>
            <td>Contratto</td><td>Costo</td><td>APN</td><td>Attivazione</td>
        </thead>
        <tbody>
            <tr ng-repeat="sim in sim_list" ng-click="ui_mgmt(1,sim)">
                <td>{{ sim.pk }}</td>
                <td>{{ sim.sim_iccid }}</td>
                <td>{{ sim.sim_operatore }}</td>
                <td>{{ sim.sim_ndati }}</td>
                <td>{{ sim.sim_pin }}</td>
                <td>{{ sim.sim_puk }}</td>
                <td>{{ sim.sim_contratto }}</td>
                <td>{{ sim.sim_costo }}</td>
                <td>{{ sim.sim_apn }}</td>
                <td>{{ sim.sim_data_attivazione }}</td>
            </tr>   
        </tbody>
    </table>
</div>
<div ng-controller="ui_controller">
    {{show_details}}
    <div ng-if="show_details==1"  class="td_mc_rightbar" >
        Dettagli...
        <div ng-switch="sub_bottom_menu">
            <div ng-switch-when="sim_mgmt">
                Blablablabla
            </div>              
        </div>
    </div>
</div>

I'm I missing something or....???

Thank you in advice :)

Andrea

1
  • 1
    If it was me, I would not pass scope to the service,.. Rather I would have methods returning the updated data $scope.someVar = service.getVar(). Also, as you do an implicit update of the data of another scope, I think you might need to do $scope.digest() Commented Dec 1, 2015 at 7:20

2 Answers 2

2

The initial value of show_details is being copied into the controller, but it's not being updated because numbers in JavaScript are passed by value, not by reference.

Since objects are pass-by-reference, an easy solution is to put the values you want to bind within an object:

ui_service_f = function() {
    this.options = {
         show_details: "-1";
    };
};
ui_controller =function($scope,ui_service) {     
    $scope.options = ui_service.options;
};

Then in your controller:

<div ng-if="options.show_details==1"  class="td_mc_rightbar" >
<!-- ... -->
</div>

This works because the line $scope.options = ui_service.options creates a reference the options object in the scope, which is then checked for updates in the usual Angular digest cycle.

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

Comments

2

When passing show_details you are passing its value and not a pointer to the object since its a simple object. Adding a single layer to it will do the trick.

this.show_details = {value: "-1"};

Here is a fiddle: http://jsfiddle.net/bwdpft1y/

Comments

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.