[AngularJS] Adding custom methods to angular.module
There are situations where you might want to add additional methods toangular.module
. This is easy to accomplish, and can be a handy technique.
- //For directive template
- <hello></hello>
- //For directive controller
- <li menu-item ng-repeat="category in categories"
- class="menu-animation"
- ng-class="{'highlight':mouse_over}"
- ng-mouseenter="mouse_over = true"
- ng-mouseleave="mouse_over = false"
- ng-class="{'active':isCurrentCategory(category)}">
- <a ng-click="setCurrentCategory(category)">
- {{category.name}}
- </a>
- </li>
- var original = angular.module;
- angular.module = function(name, deps, config){
- var module = original(name, deps, config);
- module.quickTemplate = function(name, template){
- module.directive(name, function() {
- return {
- template: template
- }
- });
- };
- module.quickController = function(name, controller) {
- module.directive(name, function() {
- return {
- controller: controller
- }
- })
- };
- return module;
- };
Use: We comment out the meunItem directive, instead using quickController method added to the end of the file.
- angular.module('categories', [
- 'eggly.models.categories',
- 'ngAnimate'
- ])
- .config(function ($stateProvider) {
- $stateProvider
- .state('eggly.categories', {
- url: '/',
- views: {
- 'categories@': {
- controller: 'CategoriesController',
- templateUrl: 'app/categories/categories.tmpl.html'
- },
- 'bookmarks@': {
- controller: 'BookmarksController',
- templateUrl: 'app/categories/bookmarks/bookmarks.tmpl.html'
- }
- }
- });
- })
- .controller('CategoriesController', function ($scope) {
- })
- /*
- .directive('menuItem', function(){
- var controller = function($scope){
- $scope.mouse_over = false;
- };
- return {
- controller: controller
- }
- })*/
- .animation('.menu-animation', function () {
- return {
- beforeAddClass: function (element, className, done) {
- if (className == 'highlight') {
- TweenLite.to(element, 0.2, {
- width: '223',
- borderLeft: '10px solid #89CD25',
- onComplete: done
- });
- TweenLite.to(element.find('a'), 0.2, {
- color: "#89CD25"
- });
- }
- else {
- done();
- }
- },
- beforeRemoveClass: function (element, className, done) {
- if (className == 'highlight') {
- TweenLite.to(element, 0.4, {
- width: '180',
- borderLeft: '5px solid #333',
- onComplete: done
- });
- TweenLite.to(element.find('a'), 0.4, {
- color: "#5bc0de"
- });
- }
- else {
- done();
- }
- }
- };
- })
- .quickController('menuItem', function($scope){
- $scope.mouse_over = false;
- })
- ;
Have to add quickController to the end of the file, otherwise, it breaks the chain.
[AngularJS] Adding custom methods to angular.module的更多相关文章
- 【angularJS】定义模块angular.module
模块定义了一个应用程序.控制器通常属于一个模块. JavaScript 中应避免使用全局函数.因为他们很容易被其他脚本文件覆盖. AngularJS 模块让所有函数的作用域在该模块下,避免了该问题. ...
- 33.AngularJS 应用 angular.module定义应用 angular.controller控制应用
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 模块(Module) 定义了 AngularJS 应用. AngularJS 控制器(Co ...
- AngularJs angular.Module模块接口配置
angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...
- AngularJs angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- angularjs ngTable -Custom filter template-calendar
jsp页面: <script type="text/ng-template" id="path/to/your/filters/top-Date-One.html& ...
- AngularJS开发指南3:Angular主要组成部分以及如何协同工作
AngularJS的主要组成部分是: 启动(startup) - 展示“hello world!” 执行期(runtime) - AngularJS 执行期概览 作用域(scope) - 视图和控制器 ...
- angular.module 详解
AngularJS 模块 模块包含了主要的应用代码. 一个应用可以包含多个模块,每一个模块都包含了定义具体功能的代码. 可以将module理解成一个容器,可以往其中放入controllers.serv ...
- Angular Module声明和获取重载
module是angular中重要的模块组织方式,它提供了将一组内聚的业务组件(controller.service.filter.directive…)封装在一起的能力.这样做可以将代码按照业务领域 ...
- [AngularJS] Extract predicate methods into filters for ng-if and ng-show
Leaking logic in controllers is not an option, filters are a way to refactor your code and are compa ...
随机推荐
- 【Java多线程】互斥
Java多线程学习2——互斥 一.前言 在上一节 (http://www.cnblogs.com/lzhen/p/3917966.html) 中,通过实现Runnable接口,可以实现多线程中的资源的 ...
- motan源码解读:注册中心zookeeper(1)
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly rel ...
- bzoj 3289 Mato的文件管理(莫队算法+BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题意] 回答若干个询问:[l,r]区间内的逆序对个数. [思路] 莫队算法,B ...
- java学习随笔--- 捣蛋vector
最近比较有时间啦,有时间搞下java,个人觉得学这门语言语法太多啦,不一一去学习啦,心血来潮,挂了个struct2的源代码,一入深似海啊,看得我天花缭乱,从最简单的开始吧 public static ...
- C++11 多线程
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...
- (转)QR二维码生成及原理
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...
- django 搭建自己的博客
原文链接:http://www.errdev.com/post/4/ 每一个爱折腾的程序员都有自己的博客,好吧,虽然我不太喜欢写博客,但是这样骚包的想法却不断涌现.博客园虽好,可以没有完全的掌控感,搭 ...
- ubuntu 错误 & 解决
1.ssh时出现“段错误(核心已转储)” 原因:说明与ssh有关的内核代码被修改过并且部分代码访问内存过界 解决:1.将内核代码被修改过的部分修改回来 2.sudo apt-get re ...
- 《Java数据结构与算法》笔记-CH4-5不带计数字段的循环队列
第四章涉及三种数据存储类型:栈,队列,优先级队列 1.概括:他们比数组和其他数据存储结构更为抽象,主要通过接口对栈,队列和优先级队列进行定义.这些 接口表明通过他们可以完成的操作,而他们的主要实现机制 ...
- 基于Linux2.6内核的加密容器法保护文件方法
本文出自 "李晨光原创技术博客" 博客,谢绝转载!