angularjs – 在Angular UI bootstrap datepicker中检测选定的月
发布时间:2020-12-17 17:15:05 所属栏目:安全 来源:网络整理
导读:我在日模式下使用 Angular UI Bootstrap日期选择器为我的项目.如果选择新日期,我怎样才能获得当前开放的月份? 解决方法 花了一些时间试图找到正确的方法来获得这个,并在阅读datepicker指令和控制器的源代码后,我找不到一个非脏的方式获得当前月份,所以这是
我在日模式下使用
Angular UI Bootstrap日期选择器为我的项目.如果选择新日期,我怎样才能获得当前开放的月份?
解决方法
花了一些时间试图找到正确的方法来获得这个,并在阅读datepicker指令和控制器的源代码后,我找不到一个非脏的方式获得当前月份,所以这是一个非常hacky方式来做到这一点.
警告,未来版本的Angular-UI Bootstrap可能会破坏这种方法 你需要使用AngularJS decorators.这是您需要的代码: angular.module('myApp',[ 'ui.bootstrap' ]) .config(function($provide) { $provide.decorator('datepickerDirective',function($delegate) { var directive = $delegate[0]; //get a copy of the directive's original compile function var directiveCompile = directive.compile; //overwrite the original compile function directive.compile = function(tElement,tAttrs) { // call the directive's compile with apply to send the original 'this' and arguments to it var link = directiveCompile.apply(this,arguments); //here's where the magic starts return function(scope,element,attrs,ctrls) { //call the original link link.apply(this,arguments); //move is the internal function called when you click //on the chevrons for previous and next month var originalMove = scope.move; scope.move = function(direction) { originalMove.apply(this,arguments); //when move is called,the 'this' object contains a 'title' property with the current month! var currentMonth = this.title; //emit the event to parent scopes scope.$emit('datepicker.monthChanged',currentMonth); } } }; return $delegate; }); }) 然后你的控制器可以监听datepicker.monthChanged: $scope.$on('datepicker.monthChanged',function(event,newVal) { $scope.currentMonth = newVal; }) 由于datepicker控制器没有将当前所选日期保留在$scope中,因此它将其保存在一个闭包变量中,当调用函数move时,我发现你可以获得所选月份的唯一方法是在this对象中.只要您单击上一个和下个月的图标,就会调用move. 这是一个使用这个装饰器的傻瓜:http://plnkr.co/edit/iWJWjM8nCsh5TMupNioo?p=preview (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- angularjs – angular-infinite $digest循环在ng
- Scalac发现错误:scala.Boolean(false)required:
- linux – 对于memcached实例,Docker容器有什么好
- scala – 类型别名和类型羊羔之间的区别
- bash – 如何将MSYS2 shell集成到Window上的Visu
- AngularJS中【Error: [$rootScope:inprog]】的解
- bash – awk系统调用
- angularjs – TextAngular fileDropHandler文档
- shell特殊符号、cut命令、sort_wc_uniq命令、tee
- Angular SPA + Jersey + SpringMVC 整合应用
热点阅读