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'       
                }
            }

        })

2 Answers 2

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.

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

9 Comments

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
you can use $scope.xyz in different views in same controller
yes, we can but the value in other views for $scope.xyz is showing empty unless and untill i used $rootScope
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
im using it cz more data to be exchange between my parent state to all other child views
|
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.

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.