angular_$inject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE HTML>
<html lang="zh-cn" ng-app="MainApp">
<head>
    <meta charset="UTF-8">
    <title>explicit-inject-service</title>
</head>
<body>
<div ng-controller="MyController">
    <input type="text" ng-model="msg"/>
    <button ng-click="saveMsg()">save msg</button>
    <ul>
        <li ng-repeat="msg in msgs">{{msg}}</li>
    </ul>
</div>>
<script src="js/angular.js" type="text/javascript"></script>
<script type="text/javascript">
    var app = angular.module("MainApp",[],function($provide) {
        $provide.factory("notify",["$window","$timeout",function(win,timeout) {
            //这里是服务依赖服务,通过这种显式的方式,参数名可以乱填,但顺序要对应
            var msgs = [];
            return function(msg) {
                msgs.push(msg);
                if(msgs.length==3) {
                    timeout(function() {
                        win.alert(msgs.join("\n"));
                        msgs = [];
                    },10);
                }
            }
        }]);
    });
 
    function MyController($s,$noti) {
        //这里是controller依赖服务,通过这种显式的方式,参数名可以乱填,但顺序要对应
        $s.msgs = [];
        $s.saveMsg  = function() {
            this.msgs.push(this.msg);
            $noti(this.msg);
            this.msg = "";
        };
    }
     
    //这个就是让controller里面的$s指向$scope,$noti指向notify这个服务
    MyController.$inject = ['$scope','notify'];
  //有显示依赖和隐示依赖
</script>
</body>
</html>

  

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/3659110.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(1848)  评论(1编辑  收藏  举报

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示