Javascript 建议在自定义指令前面加上';ng';在AngularJS?

Javascript 建议在自定义指令前面加上';ng';在AngularJS?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我用AngularJS编写了一个小指令来显示用户信息,下面是我编写的代码 angular.module('app').directive('userProfile', function(){ return { templateUrl: "userProfile.html", restrict: 'E', } }); 我在我的index.htmlas中包含了这个指令 <body ng-controller="myCtrl"> <

我用AngularJS编写了一个小指令来显示用户信息,下面是我编写的代码

angular.module('app').directive('userProfile', function(){
    return {
      templateUrl: "userProfile.html",
      restrict: 'E',
    }
});
我在我的
index.html
as中包含了这个指令

  <body ng-controller="myCtrl">
    <user-profile><user-profile>
  </body>

如果我想使用
ng-
前缀在整个项目中保持某种统一的结构,现在我已经尝试将
.directive('userProfile'
声明更改为
.directive('ngUserProfile'
),这实际上是有效的

但是,我想知道是否建议模仿
ng-
行为,如果是,是否有我可以利用的最佳实践

请帮忙

提前感谢,,
戴夫

用ng作为前缀不是一个好主意

ng模块默认在AngularJS应用程序启动时加载。该模块本身包含AngularJS应用程序运行的基本组件


强烈建议不要为我们开发的自定义组件(指令/过滤器)添加ng前缀,因为它将来不会与任何角度组件冲突。

因为HTML有角度括号,“ng”听起来像“角度”
ng-
前缀是指令的预定义角度前缀。
所以,如果您正在寻找自定义指令,您可以根据需要使用前缀中的任何内容

.directive("myDir", function() { 
  // logic
})
前缀
ng
代表“Angular”;所有附带Angular的内置指令都使用该前缀。
同样,建议您不要在自己的指令上使用
ng
前缀,以避免将来版本的Angular中可能出现的名称冲突


完成此操作

不要使用ng前缀

这是AngularJS指令的命名标准,最好不要使用它,以免与海关指令混淆。

David R

我建议你阅读约翰·帕帕的《AngularJS风格指南》

是关于为自定义指令提供前缀的说明

(其不良做法)不要为自定义指令使用
ng-
前缀,因为angular使用
ng-
惯例,这可能会导致将来与angular指令发生冲突。最佳做法是拥有自己的前缀,如
ms user profile