如何使用Angularjs检查指令或控制器在模块中是否可用 [英] How to check if a directive or controller is available in a module using Angularjs

查看:54
本文介绍了如何使用Angularjs检查指令或控制器在模块中是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在angularjs中,给定模块,如何检查给定模块是否存在指令/控制器.

In angularjs, given a module, how do you check if a directive/controller exists given a module.

我有一个模块,我想知道是否已加载某些特定指令.下面是一些示例代码:

I have a module and I want to know if some particular directives have been loaded. Below is some sample code:

var module = angular.module('myModule');
//check if controller exists
if (module.hasController('my.first.controller')){
   //do something
}
if (module.hasDirective('my.first.directive')){
   //do something
}

我已经以某种方式实现了这一点.寻找默认情况下可用的更好方法.

I have implemented this in a way. Looking for a better way of doing it if it is available by default.

这可能吗? 如果是这样,您该怎么做?

Is this possible? If so, how do you do this?

推荐答案

使用此代码检查服务是否存在.

Use this code to check if a service exists.

$injector.has('myServiceName')

要检查指令是否存在,必须在指令名称后添加Directive后缀:

To check if a directive exists, you must add a Directive suffix after the directive name:

$injector.has('myDirectiveNameDirective')

这篇关于如何使用Angularjs检查指令或控制器在模块中是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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