【问题标题】:How to get current Controller $scope inside directive function in angularjs?如何在 angularjs 的指令函数中获取当前控制器 $scope?
【发布时间】:2016-02-16 05:04:15
【问题描述】:

示例链接:Example

大家好,

我在基于 url 的索引文件中创建了一个全局帮助按钮,正在加载动态帮助(html 文件),它工作正常。我需要确定哪个控制器被激活,并且我需要将当前的 $scope 作为参数传递给 $scope.HelpFunction function.$scope.HelpFunction 在帮助指令中可用。在指令中,我需要从当前控制器获取当前 $scope.message。基于 $scope 我需要实现一些逻辑,任何人都可以帮助我.....

  .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)
  };

})

【问题讨论】:

  • 我尝试了同样的结果。可以分享一下代码吗?
  • “激活”控制器是什么意思?

标签: javascript angularjs angularjs-directive angularjs-scope angular-ui-router


【解决方案1】:

创建一个带有单例的工厂,并将当前范围的引用保存在每个控制器中。

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

   return currentScope;
})

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

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

访问指令中的当前范围。

.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){

      }
    }
  })

您必须为您的视图分配一个控制器。

$stateProvider.state('Home', {
                url: '/home',
                templateUrl: 'Home.html',
                controller: 'HomeController'
            });

【讨论】:

  • 总是只返回 {} 对象@Charlie H
  • 请正确实施此方案。然后它不会返回 {}。
  • 我实现了你提到的@charlie H
  • 你能给我可运行的链接吗?
  • 通过$stateProvider 将控制器分配给您的模板。这样你的控制器就会被执行,currentScope 工厂也会被更新。答案已更新。
【解决方案2】:

像这样将scope: true 添加到您的returnig 数组中

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

更新:

外部示例http://jsfiddle.net/5gWjY/671/

更新 2(根据要求):

外部示例http://jsfiddle.net/5gWjY/673/

【讨论】:

  • 在 $scope.HelpFunction 中我需要访问当前的 $scope
  • $scope.HelpFunction = function(id) { $scope; }?
  • 请查看示例
  • 您可以尝试使用多个页面和控制器吗? @Nevershowmyface
  • 好的,我会编辑它并发布一个更新的 2。顺便说一句,它对你有用吗?
【解决方案3】:

要解决这个问题,您可以使用serviceservice 将在controllers 之间共享数据。

plunker 上的实时示例。

创建service

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

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) {

  }
}
})

controller

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

【讨论】:

  • 这看起来更像是一种解决方案,而不是实际的解决方案,抱歉
  • 这是行不通的。此解决方案 - angular 方式。 Services 存在于 之间 controllers 之间的数据分布。 SO就是这样一个问题——如何在不同的controllers之间共享数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
相关资源
最近更新 更多