Angular__Module

前端之家收集整理的这篇文章主要介绍了Angular__Module前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AngularJS.module方法返回一个Module对象。Module对象定义的方法返回的结果仍然是Module对象,因此我们可以使用连缀写法。

Module对象的成员方法

name  //返回模块名称
animation(name,factory)
config(callback) //注册一个在模块加载时进行配置的函数

angular.module("exampleApp.Service",[])
    .service("days",function(nowValue){
        this.today = nowValue.getDay();
        this.tomorrow = this.today + 1;
    })
    .config(function(){
        console.log("Service module config: " + "(no time)");
    })
    .run(function(startTime){
        console.log("Service module run: " + startTime);
    });

config方法接收一个函数,该函数调用方法的模块被加载后调用,常通过注入来自其它服务的值的方式用于配置模块。

run(callback) //注册一个在AngularJS加载完毕后,对所有模块进行配置的函数

run方法函数在==所有模块==加载完成后以及解析完它们的依赖后才会被调用

constant(key,value) //定义一个返回常量的服务

myApp.constant("startTime",new Date().getTime());
value(name,value)

var now = new Date();
myApp.value("nowValue",now);
filter(name,factory)

angular.module("exampleApp.Filters",[]).filter("dayName",function(){
    return function(input){
        return XXX;
    }
})

filter用来格式化展示给用户的数据,一旦定义过滤器后,就可以在整个模块中应用。

在视图中使用过滤器,在数据绑定或者表达式后紧跟一个竖线(“|”)以及过滤器的名称

@H_403_124@ filter访varmyFilter= filter(“dayName”);

directive(name,factory)

angular.module("exampleApp",[])
    .directive("highlight",function($filter){
        var dayFilter = $filter("dayName");
        return {
            restrict: "AE",link: function(scope,element,attrs){
                if(dayFilter(scope.day) == attrs["highlight"]){
                    element.css("color","red");
                }
            }
        }
    });

注意是==scope==,而不是$scope.

scope用于检查在试图中可用的数据,attrs参数提供了指令所应用到的元素的属性的完整集合,包括让指令起作用的那个属性。element是一个jqLite对象。

controller(name,constructor)

var myApp = angular.module("myApp",[]);
myApp.controller("myCtrl",function($scope){

});

控制器的命名习惯是使用Ctrl后缀。

provider(name,constructor) //定义一个服务
service(name,constructor) angular.module("exampleApp.Service",[]) .service("days",function(nowValue){ this.today = nowValue.getDay(); this.tomorrow = this.today + 1; })
factory(name,provider) //定义一个服务
原文链接:https://www.f2er.com/angularjs/149170.html

猜你在找的Angularjs相关文章