-1

As displayed below, even though i've 2 diff html i am using same controller so i want to have the scope shared between 2 html.

Is it possible to configure this in routeProvider?

.when('/cform', {
        templateUrl: '/partials/clientForm.html',
        controller: 'ClientCtrl', //Same Controller
   })
   .when('/vwClientManagement', {
        templateUrl: '/partials/clientManagement.html',
        controller: 'ClientCtrl' //Same Controller
   })


$scope.showClientAddUpdateView = function(action,clientId) {
    $scope.client.name="Hello World"; // This is getting displayed in UI.
        $http.get("/qCli", { params:{id : clientId}}).then(
            function successCallback(response){
                //$scope.client = response.data;
                $scope.client.name="Hello World After Ajax"; // This is not getting displayed in UI.
                $location.path('/cform');
            },function errorCallback(response){
                console.log("err searching clients");
            }
        );
};

Update: Scanario 1 - changing route and setting scope in ajax success call back, loosing only those value which i have set in success callback after route change

Scanario 2 - updating scope in ajax success callback but not changing route, view is updated with correct values.

Added More details More Details

Community
  • 1
  • 1
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60
  • 1
    `"i want to have the scope shared..."`.. you should use `service` for same. otherwise, you will run into problems. because `Controllers are disposed when changing routes`. data will be reset – anoop Apr 04 '17 at 13:54
  • Service has nothing to do with scope. No you are wrong, they don't get disposed, i am using only single controller for multiple route but still the scope variable is shared even though i change the route. Everytime i change the route and come back to same old route i have to clear the scope variable to clear the values of forms. – Jigar Naik Apr 04 '17 at 13:57
  • [please read](http://stackoverflow.com/questions/16210822/angular-js-views-sharing-same-controller-model-data-resets-when-changing-view), It would be ok if you would have used `state` base routing (`ui-router`), which you are not using. – anoop Apr 04 '17 at 14:07
  • Thanks i read that post, i can see values which are set before ajax call but can't see the values which are set on success callback of the ajax call. As per the link shared by you after routing it should not display any of the values set in the controller since its gonna br a new instance. – Jigar Naik Apr 04 '17 at 23:44

2 Answers2

1

In my opinion, it's better to use Service, for sharing data between Angular controllers. Generally, angularjs is very flexible, and there is at least few ways to solve your problem: using services, using $state.go services, using stateparams, using rootscope...

Check this issue, there is a bunch of related and useful advices: Share data between AngularJS controllers

Community
  • 1
  • 1
Dmytro Medvid
  • 4,988
  • 3
  • 25
  • 29
0

While using this:

.when('/cform', {
    templateUrl: '/partials/clientForm.html',
    controller: 'ClientCtrl', //Same Controller
}).when('/vwClientManagement', {
    templateUrl: '/partials/clientManagement.html',
    controller: 'ClientCtrl' //Same Controller
})

You are not using one unique controller. You are creating, for each view, an instance of ClientCtrl.

So they don't share there scope, each have a different and independant scope.

It seems you are trying to avoid using services whereas this would be best done in this case. There are severals (1, 2) answers on Stack Overflow dealing with this subject.

Community
  • 1
  • 1
Mistalis
  • 17,793
  • 13
  • 73
  • 97
  • If that is the case why whatever i set in score before ajax call is being displayed in view after routing !!! I tried using service also which returns promise object to controller. That also does not work for me. – Jigar Naik Apr 04 '17 at 23:35