angularjs share data config between controllers

后端 未结 4 960
予麋鹿
予麋鹿 2020-11-28 15:52

I\'m wondering what could be a good way to share directive between controller. I\'ve got ie two directives to use in different controller with different configuration the fi

4条回答
  •  情话喂你
    2020-11-28 16:21

    obvious but brilliant solution (may be)

    (function(window, angular, undefined) {
                    'use strict';
                    angular.module('ctrl.parent', [])
                        .run(function ($rootScope) {
                            $rootScope.test = 'My test'   
                            $rootScope.myTest = function(){
                                alert('It works');
                            }
                    });
                })(window, angular);
                angular.module('app',['ctrl.parent'])
                    .controller('ChildCtrl', function($scope){
    
                });
    

    It's easy and clean and don't see any drawback(it's not global)

    UPDATE

    'use strict';
                    (function(window, angular, undefined) {
                        'use strict';
                        angular.module('ctrl.parent', [])
                            .controller('ParentController',function (scope) {
                                scope.vocalization = '';
                                scope.vocalize = function () {
                                    console.log(scope.vocalization);
                                };
                        });
                    })(window, angular);
                    angular.module('app',['ctrl.parent'])
                        .controller('ChildCtrl', function($scope,$controller){
                        angular.extend($scope, new $controller('ParentController', {scope:$scope}));
    $scope.vocalization = 'CIP CIP';
                    });
    

    just a little neater and it works CIP CIP :)

提交回复
热议问题