1

app.js

I have created multiple views having the same controller named as vendorCtrl, as we know that $scope can be used withing a specific controller, my question is without the help of $rootscope how can i share data between multiple views named as basic, areas, identity

$stateProvider
        .state('dashboard.vendor.add',{
            views:{
                '@': {
                    templateUrl: 'templates/vendor/forms/basic.html',
                    controller : 'vendorCtrl'   
                }
            }
        })


        .state('dashboard.vendor.add.areas',{
            views:{
                '@': {
                    templateUrl: 'templates/vendor/forms/areas.html',
                    controller : 'vendorCtrl'   
                }
            }
        })



        .state('dashboard.vendor.add.identity',{
            views:{
                '@': {
                    templateUrl: 'templates/vendor/forms/identity.html',
                    controller : 'vendorCtrl'       
                }
            }

        })
Taie
  • 1,021
  • 16
  • 29
md-5h04I3
  • 214
  • 2
  • 12

2 Answers2

2

Issue regarding scope inheritance

app.js

$stateProvider
    .state('dashboard.vendor.add',{
        views:{
            '@': {
                templateUrl: 'templates/vendor/forms/basic.html',
                controller : 'vendorCtrl'   
            }
        }
    })


    .state('dashboard.vendor.add.areas',{
        views:{
            '@': {
                templateUrl: 'templates/vendor/forms/areas.html',
                controller : 'vendorCtrl'   
            }
        }
    })

controller

function vendorCtrl($scope) {
   $scope.data= 'hello world';
}

areas.html

<input type-"text" ng-modal="data"/> <label name="name">{{data}}</label>

basic.html

<label name="name">{{data}}</label>

In this situation value of $scope.data would be display in areas.html but failed to display value in basic.html despite having same controller. Because scope properties only inherit down the state chain if the views of our states are nested. Inheritance of scope properties has nothing to do with the nesting of our states and everything to do with the nesting of your views.

md-5h04I3
  • 214
  • 2
  • 12
ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41
  • thats ok, we need to create a factory but my doubt is why being on the same controller $scope.xyz giving empty values for other views – md-5h04I3 Sep 07 '16 at 10:59
  • you can use $scope.xyz in different views in same controller – ojus kulkarni Sep 07 '16 at 11:04
  • yes, we can but the value in other views for $scope.xyz is showing empty unless and untill i used $rootScope – md-5h04I3 Sep 07 '16 at 11:06
  • may be there is wrong in some other part of code, or share your controller code so i ll lookout. Any ways, why you are using same controller for child view.? controller should be different for child view. it is a bad practice to write parent and child in one controller – ojus kulkarni Sep 07 '16 at 11:08
  • im using it cz more data to be exchange between my parent state to all other child views – md-5h04I3 Sep 07 '16 at 11:23
  • No add controller where you used $scope.? and go through this link https://docs.angularjs.org/guide/controller – ojus kulkarni Sep 07 '16 at 11:25
  • Yes but this is not the proper way to share data in child views. you have to declare separate controllers for every view and if you want to use sharing logic then only uise services – ojus kulkarni Sep 07 '16 at 11:33
  • :thanks, i got the issue, its the scope inheritance that is creating problem – md-5h04I3 Sep 08 '16 at 11:41
  • Ok.! if possible then edit my answer accordingly, so it will be right answer for your question. – ojus kulkarni Sep 08 '16 at 11:54
0

On the one hand, With ui-router you can use the params property.

On the other hand, you can use a simple (getter/setter ?) service that can hold your data.

Community
  • 1
  • 1
Zakaria
  • 14,892
  • 22
  • 84
  • 125