发布于 2015-05-23 11:55:18 | 156 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的AngularJS Tutorial中文版,程序狗速度看过来!

AngularJS 前端JS框架

AngularJS诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入,等等。


What is an AngularJS service or factory?

Singleton.

Yes! That one word is enough to define AngularJS services. The purpose of AngularJS service / factory function is to generate a single object or function that represents the service to rest of the application. That object or function is passed as a parameter to any other factory function which specifies a dependency on this service.

Services

Syntaxmodule.service(‘serviceName‘, function);

Result: When declaring serviceName as an injectable argument you will be provided with the instance of a function passed to module.service.

Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call(this) or similar.

示例:

varapp = angular.module(‘myApp‘, []);
     
// Service definition
app.service(‘testService‘, function(){
    this.sayHello= function(text){
        return"Service says "Hello "+ text + """;
    };       
});
 
// AngularJS Controller that uses the service
functionHelloCtrl($scope, testService)
{
    $scope.fromService = testService.sayHello("World");
}

Factories

Syntaxmodule.factory(‘factoryName‘, function);

Result: When declaring factoryName as an injectable argument you will be provided the value that is returned by invoking the function reference passed to module.factory.

Usage: Could be useful for returning a ‘class’ function that can then be new’ed to create instances.

 
varapp = angular.module(‘myApp‘, []);
 
// Factory
app.factory(‘testFactory‘, function(){
    return{
        sayHello: function(text){
            return"Factory says "Hello "+ text + """;
        } 
    }              
});
 
// AngularJS Controller that uses the factory
functionHelloCtrl($scope, testFactory)
{
    $scope.fromFactory = testFactory.sayHello("World");
}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务