Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Sample Link : Example

Hi all,

I created one Global Help button in index file based on url am loading dynamic help(html file) which is working fine. I need to identify which controller is activated and i need to pass current $scope as parameter to $scope.HelpFunction function.$scope.HelpFunction available in help directive. In directive I need to get current $scope.message from current controller. based on $scope i need to implement few logic any one help me on this.....

  .directive("help", function($location, $window){
    .directive("help", function($location, $window){
    return {
      restrict: "A",
      template: "<button   ng-click=\"HelpFunction( )\">Help</button>",
      scope: true,
      controller: function($scope){
          $scope.HelpFunction = function( ){
            //Here I need to access current  $scope value
            alert($scope.message)
               //var url =$location.url();
               //$window.open(url+ '.html', '', 'width=700,height=700');
          }
      },
      link: function(scope){

      }
    }
  })
.controller('HomeController', function ($scope, router, $location) {
        $scope.message = "Home";
       $scope.TestFunction = function(data) {
        console.log("Home:" + $location.url())
  };

})
.controller('MainController', function ($scope, router) {
        $scope.message = "Main";
       $scope.TestFunction = function(data) {
        console.log("Main:" + data)
  };

})
 .controller('Page1Controller', function ($scope, router) {
    $scope.message = "Page1";
  $scope.TestFunction = function(data) {
       console.log("Page2:" + data)
  };

})
share|improve this question
    
I tried same result. can u share coding? – Suresh B Feb 16 '16 at 5:16
    
What do you mean by "activating" a controller? – Charlie H Feb 16 '16 at 6:29
up vote -1 down vote accepted

To solve this problem, you can use the service. The service will be share data between controllers.

Live example on plunker.

Create service.

.service('HelpService',function(){
 return {
   message:""
 }
})

In directive.

.directive("help", function($location, $window) {
return {
  restrict: "A",
  template: "<button   ng-click=\"HelpFunction( )\">Help</button>",
  scope: true,
  controller: function($scope,HelpService) {
    console.log($scope)
    $scope.HelpFunction = function() {
      //Here I need to access current  $scope value
      alert(HelpService.message)
        //var url =$location.url();
        //$window.open(url+ '.html', '', 'width=700,height=700');
    }
  },
  link: function(scope) {

  }
}
})

In controller.

 .controller('HomeController', function($scope, router, $location,HelpService) {
HelpService.message = "Home";
console.log(HelpService.message)
$scope.TestFunction = function(data) {
  console.log("Home:" + $location.url())
};
})
share|improve this answer
1  
This looks more like a work around than the actual solution, sorry – Nevershowmyface Feb 16 '16 at 6:47
    
This is not work around. This solution - angular way. Services exist for the distribution of data between controllers. SO is just such a question - how to share the data between different controllers. – Stepan Kasyanenko Feb 16 '16 at 6:57

Create a factory with a singleton and save the reference of your current scope in it in each controller.

factory('currentScope', function(){
   var currentScope = {};

   return currentScope;
})

controller('HomeController', function($scope, currentScope) {
   currentScope = $scope;   
})

controller('MainController', function($scope, currentScope) {
   currentScope = $scope;
})

Access the current scope inside your directive.

.directive("help", function($location, $window, currentScope){
    return {
      restrict: "A",
      template: "<button   ng-click=\"HelpFunction( )\">Help</button>",
      scope: true,
      controller: function($scope){
          $scope.HelpFunction = function( ){
            //Here I need to access current  $scope value
            console.log(currentScope);

            alert($scope.message)
               //var url =$location.url();
               //$window.open(url+ '.html', '', 'width=700,height=700');
          }
      },
      link: function(scope){

      }
    }
  })

You will have to assign a controller to your view.

$stateProvider.state('Home', {
                url: '/home',
                templateUrl: 'Home.html',
                controller: 'HomeController'
            });
share|improve this answer
    
Always return {} object only @Charlie H – Suresh B Feb 16 '16 at 6:41
    
Please implement this solution correctly. It will not return {} then. – Charlie H Feb 16 '16 at 6:44
    
I implemented same what ur mentioned @charlie H – Suresh B Feb 16 '16 at 7:04
    
can u give me runable link? – Suresh B Feb 16 '16 at 7:05
    
Assign a controller to your template via $stateProvider. This way your controller is executed and the currentScope factory is updated. Answer updated. – Charlie H Feb 16 '16 at 8:08

Add scope: true to your returnig array like this

module.directive("help", function($location, $window){
  return {
    restrict: "A",
    scope: true, // Get the scope of the current Controller
    template: "<button style=\"background-color: #f4f4f4;  color: black;\" ng-click=\"HelpFunction(\'section2\')\"  my-target>Help</button>",
    controller: function($scope) {
      $scope.HelpFunction = function(id) {
        var url = $location.url();
        $window.open(url+ '.html', '', 'width=700,height=700');
      }
    }
  }
})

UPDATED:

External example http://jsfiddle.net/5gWjY/671/

UPDATED 2 (as requested):

External example http://jsfiddle.net/5gWjY/673/

share|improve this answer
    
Can you share runable example @Nevershowmyface – Suresh B Feb 16 '16 at 5:29
    
inside $scope.HelpFunction I need to access current $scope – Suresh B Feb 16 '16 at 5:31
    
$scope.HelpFunction = function(id) { $scope; }? – Nevershowmyface Feb 16 '16 at 5:39
    
Not working ...@ Nevershowmyface – Suresh B Feb 16 '16 at 5:43
    
Check the example please – Nevershowmyface Feb 16 '16 at 6:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.