angular 手动编译_Angular - - $compile编译服务与指令

$compile服务在Angular中用于编译HTML模板,与指令交互。本文深入探讨了指令的定义对象,包括priority、template、scope、controller等属性,以及link函数、编译流程和$compile的使用方法。通过实例展示了如何在项目中应用这些概念。
摘要由CSDN通过智能技术生成

$compile

这是个编译服务。编译一段HTML字符串或者DOM的模板, 产生一个将scope和模板连接到一起的函数。

编译服务主要是为指令编译DOM元素,下面的一大段也是主要介绍指令的。

下面是一个被声明的带指令定义对象的指令的示例:

var myModule =angular.module(...);

myModule.directive('directiveName', [“injectables”,…,functionfactory(injectables,…) {

var directiveDefinitionObject ={

priority: 0,

template: '

//or

//templateUrl: 'directive.html', // or // function(tElement, tAttrs) { ... },

replace: false,

transclude: false,

restrict: 'A',

scope: false,

controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... },

controllerAs: 'stringAlias',

require: 'siblingDirectiveName', //or // ['^parentDirectiveName', '?optionalDirectiveName', '?^optionalParent'],compile: functioncompile(tElement, tAttrs, transclude) {

return{

pre: functionpreLink(scope, iElement, iAttrs, controller) { ... },

post: functionpostLink(scope, iElement, iAttrs, controller) { ... }

}

//or

//return function postLink( ... ) { ... }

},

//or

//link: {

//pre: function preLink(scope, iElement, iAttrs, controller) { ... },

//post: function postLink(scope, iElement, iAttrs, controller) { ... }

//}

//or

//link: function postLink( ... ) { ... }

};

returndirectiveDefinitionObject;

});

这是一个完整的指令,所返回的对象的各项属性都写在上面了,下面对于指令对象属性的介绍,就不一一的写示例代码了,后面主要写下简单的就ok。

备注:任何为指定的参数都将设置为默认值。下面将会列出各默认值。

指令定义对象

指令定义的对象为编译器提供说明,其属性是:

priority

当有一个DOM元素中定义了多个指令,有时有必要指定的指令的应用顺序。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值