发表于: 2017-07-12 23:30:02
1 796
任务进展:
1、查看directive部分,augular自定义封装指令;
2、尝试写搜索功能的demo;
遇到问题:
1、在directive中在嵌套一个directive,前面的指令就会覆盖掉后面的内容,仔细看资料后,发现有个transclude配置项,需要将transclude配置项的值设置为true,然后就可以实现指令嵌套了;
明天计划:
1、学习directive;
2、继续写任务;
收获心得:
1、关于directive加载顺序的:
directive加载步骤概述
var myModule = angular.module("MyModule",[]);
myModule.directive("hello",function(){
return {
restrict:'AECM',
templateUrl:"templateUrl.html",
//templateUrl 不需要在 JS 中写过多的HTML标签,通过 templateUrl 将HTML模板引入进来
replace:true,
compile:function () {
//compile属性一般不常用,常用的是下面的link
},
link:function () {
}
//link阶段,对每一条指令运行link函数;
//link函数一般用来操作DOM、绑定事件监听器,还可以绑定scope作用域;
};
});
2、指令directive如何调用控制器controller定义的一些方法;指令与控制器的交互;
var myModule = angular.module("MyModule",[]);
myModule.controller('MyCtrl',['$scope',function ($scope) {
$scope.loadData=function () {
console.log("数据加载中...");
}
}]);
myModule.controller('MyCtrl2',['$scope',function ($scope) {
$scope.loadData2=function () {
console.log("数据加载中...222");
}
}]);
myModule.directive("loader",function () {
return{
restrict:'AE',
//directive通过link方法调用上面controller定义的一些方法,具体如下;
link:function (scope,element,attr) {
element.bind("mouseenter",function () {
// scope.loadData();
// scope.$apply("loadData()");//这是第二种方法指令调用controller的方法,上面是第一种,
scope.$apply(attr.howtoload);//一个指令绑定到多个controller上,需要在HTML同步定义,看到这里请翻页到HTML页看具体定义细节;
//这里跟单个绑定的写法不同,不需要写成函数的形式,写成属性的形式即可
//此处有个小坑,HTML里写的属性如果是驼峰法,那么JS里要写成小写的,小坑失足,也成大恨!
})
}//实现指令 directive 复用,指令一定要可以用在不同的controller中,为了在不同的controller中使用相同的指令
}
});
评论