发布于 2015-05-23 11:55:18 | 156 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的AngularJS Tutorial中文版,程序狗速度看过来!
AngularJS 前端JS框架
AngularJS诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入,等等。
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.
Syntax: module.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");
}
Syntax: module.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");
}