demo:
在应用中自定义一个月份选择器指令,这个指令在首页中有连个地方用到:
//这是第一次使用
<p on-month-select class=''></p>
//这是第二次使用
<p on-month-select class=''></p>
指令的html简化后是这样
<p>
<select ng-model="month" ng-change='selectUpdate()'>
<option value='1月'>1</option>
<option value='2月'>2</option>
</select>
</p>
指令的逻辑简化后是这样:
app.directive('onMonthSelect', function () {
return {
restrict: "AE",
templateUrl:'directives/month.html',
replace: true,
link: function( scope, element, attrs ) {
console.log('some thing');
}
};
});
在控制中,函数selectUpdate监听select下拉框的变化,在selectUpdate的逻辑中,如何知道当前激活的是哪个下拉框?因为页面中针对不同的下拉框,有一些独特的业务逻辑需要加进去
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
指令是可以重复使用的,不应该与业务逻辑绑定。可以暴露接口,执行回调函数来处理业务逻辑。
ng-model=""绑定,{{}}获取啊。初始化写在controller,逻辑的话依赖注入去写,这样安全些。