首页 > Web开发 > 详细

Angularjs[14] - $provide 中 factory, server 方法

时间:2017-01-27 07:07:00      阅读:280      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div ng-app="myApp">
        <div ng-controller="firstController">
            {{name}}
        </div>
    </div>

<script type="text/javascript" src="../../vendor/angular/angularjs.js"></script>
    <script type="text/javascript" src="app/index.js"></script>
</body>
</html>
var myApp = angular.module(‘myApp‘,[],function ($provide) {
    //自定义服务
    $provide.provider(‘CustomService‘,function () {
        this.$get = function () {
            return{
                message : ‘CustomService Message‘
            }
        }
    });
    //自定义工厂
    $provide.factory(‘CustomFactory‘,function () {
        return [1,2,3,4,5,6,7,8];
    });
    //自定义服务
    $provide.service(‘CustomService2‘,function () {
        return ["shanghai"];
    })
});

myApp.controller(‘firstController‘,function ($scope,CustomFactory,CustomService2) {
    $scope.name = ‘Alrale‘;
    console.log(CustomFactory);
    console.log(CustomService2);
});

技术分享

  • factory 方法直接把一个函数当成是一个对象的 $get() 方法,返回内容可以是任何类型。
  • service 方法和 factory 类似,但返回必须为对象

Angularjs[14] - $provide 中 factory, server 方法

原文:http://www.cnblogs.com/bky-1083/p/6352280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!