Skip to Content
AngularJS2 Angular 风格规则

2 Angular 风格规则

将 $ 保留给 Angular 的属性和服务

不要使用 $ 作为您自己的对象属性和服务标识符的前缀。这种命名方式应视为 AngularJS 和 jQuery 的保留风格。

正确:

$scope.myModel = { value: 'foo' } myModule.service('myService', function() { /*...*/ }); var MyCtrl = function($http) {this.http_ = $http;};

错误:

$scope.$myModel = { value: 'foo' } // 错误 $scope.myModel = { $value: 'foo' } // 错误 myModule.service('$myService', function() { ... }); // 错误 var MyCtrl = function($http) {this.$http_ = $http;}; // 错误

为什么? 区分 Angular / jQuery 内置功能和您自己添加的内容是很有用的。此外,$ 在 JS 风格指南中不是一个可接受的变量名字符。

自定义元素(Custom Element)

对于自定义元素(例如 <ng-include src="template"></ng-include>),IE8 需要特殊支持(类似 html5shiv 的 hack)才能启用 CSS 样式。在针对旧版 IE 的应用中请注意此限制。

Last updated on