获取angularjs中正在使用的当前控制器 [英] Get current controller in use in angularjs

查看:250
本文介绍了获取angularjs中正在使用的当前控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我们的Angularjs应用程序中记录不同功能的使用情况. 当前正在使用的当前功能通常由浏览器中的当前路由/URL标识,但是有些功能可以在模式/弹出窗口中打开或使用ngInclude打开,而无需更改路由/URL.

I want to log the usage of different features in our Angularjs application. Current feature in use is usually identified by the current route/url in browser but there are some features which open in a modal/popup or with ngInclude without change in route/url.

是否有任何集中的方式来识别当前正在使用的功能,我想到了要使用当前的顶级控制器,但不幸的是不知道如何获得它.

Is there any central way to identify which feature is in use currently, i thought of getting the current top level controller in use but unfortunately don't know how to get it.

还有其他目的吗?

P.S.我正在使用ngRoute.

P.S. I am using ngRoute.

推荐答案

我看到几种从/从控制器记录信息的方法.

I see several way to log infos from/of the controller.

来自 element.controller() 的文档:

From the docs of element.controller():

controller(name) -检索当前元素或其父元素的控制器.默认情况下,检索与 ngController指令.如果name作为camelCase提供 指令名称,则该指令的控制器为 检索到(例如'ngModel').

controller(name) - retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved (e.g. 'ngModel').

您可以在非常元素上使用此功能来获取其父控制器.这是使用指令的片段示例:

You can this function on very element to get its parent controller. Here is an snippet example using a directive:

var app = angular.module('app', [])
app.controller('MainController', function MainController() {});
app.controller('directiveCtrl', function directiveCtrl() {});

app.directive('controllerName', function($timeout) {
    return {
      restrict: 'A',
      template: '<div>My controller name is: {{cName}}</div>',
      controller: 'directiveCtrl',
      link: function($scope, elem, attrs) {
        var name;
        name = elem.controller().constructor.name;
        $scope.cName = name;
      }
    };
  });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<div ng-app="app">
  <div ng-controller="MainController">
    <div controller-name></div>
  </div>
  ***************************************************
  <div ng-controller="MainController">
    <div ng-controller="directiveCtrl">
      <div controller-name></div>
    </div>
  </div>
  <hr/>
</div>

您可以从Web控制台访问每个Angular对象.例如,键入angular.element($0).scope()将获得当前范围.

You can access to every Angular object from your web console. For example, typing angular.element($0).scope() will get the current scope.

请参见此答案,关于控制台的使用非常完整.

See this answer, which is very complete about the use of the console.

您可以在代码的开头在控制器中添加控制台调用,并记录所需的内容.这样,您每次知道控制器时都会知道:

You can add a console call in your controller at the beginning of the code, and log whatever you want. This way, you will know each time a controller is instanciated:

app.controller('MyCtrl', function($scope) {
    $scope.greetings = 'Hello world';
    console.log('MyCtrl instanciated!', $scope.greetings);
});

这篇关于获取angularjs中正在使用的当前控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆