angular-$rootScope与$scope-在多个控制器中进行全局诗句的初始化-run()方法使用

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/lib/AngularJS/angular.min.js"></script>
</head>
<body>
    <div ng-controller="c1">
        <div ng-bind="nickname"></div>
        <div ng-bind="username"></div>
        <div ng-bind="realname"></div>
    </div>
    <hr>
    <div ng-controller="c2">
        <div ng-bind="nickname"></div>
        <div ng-bind="username"></div>
        <div ng-bind="realname"></div>
    </div>
    <hr>
    <div ng-controller="c3">
        <div ng-bind="nickname"></div>
        <div ng-bind="username"></div>
        <div ng-bind="realname"></div>
    </div>
<script>
    var app = angular.module("myApp", []);


    // 三个控制器中,都要使用到一个数据:用户昵称~: nickname=董小姐
    /*
    为了方便多个控制器中的公共数据初始化
    AngularJS提供了一个全局作用域对象$rootScope
    AngularJS应用中,仅有一个全局作用域,其他所有的$scope作用域都是$rootScope的子元素
     */
    /*
        使用$rootScope,可以用于初始化全局变量和函数
        但是存在一个问题: 因为控制器函数中可以直接使用$rootScope,就有可能会造成全局数据污染
        规范:$rootScope一般和Angular模块的run()方法一起使用。来进行全局数据的初始化
     */
    app.controller("c1", function($rootScope, $scope) {
        $scope.nickname = "董小姐";
        $rootScope.realname = "曾大神";
 
    });
    app.controller("c2", function($scope) {
        $scope.nickname = "董小姐";
    });
    app.controller("c3", function($rootScope, $scope) {
        $scope.nickname = "董小姐";
    });


    /* 专门用于初始化全局数据 */
    app.run(function($rootScope) {
        $rootScope.username = "大刘帅哥";
    });
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
!DOCTYPE html> <html 85.3333px;"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta http-equiv="Cache-Control" content="no-transform"> <meta name="applicable-device" content="mobile"> <meta http-equiv="Cache-Control" content="no-siteapp"> <title>【元旦倒计时】2020新年倒计时_距离2020年还有多少天</title> <style> .timebox2{width:640px;background-color:#FCF3E2;padding: 1rem 0;background: url(images/ks_bcg2.png) center center no-repeat;background-size: 100% 100%;} .timebox2 div,timebox2 span,timebox2 div span{color: #333 !important;} .timebox2 .timecounter span{color: #333 !important;} .timetext{width: 100%;text-align: center;} .timetexttitle{font-size:0.3rem;margin-bottom: 10px;color: #fff;} .timetextintro{font-size: 0.22rem;margin-bottom: 10px;color: #fff;padding: 0 1.52rem;box-sizing: border-box;} .timecounter{font-size: 0.6rem;} .txt{padding: 0 0.2rem;box-sizing:border-box;} .clearbg5{display: block;clear: both;width: 100%;height: 0.16rem;overflow: hidden;background-color: #eee;} .txt p{line-height: 30px;margin-top: 0.2rem;} .txt p img{width: 100%;} .timecounter span{color: #fff;} </style> <body> 2020年元旦倒计时 元旦时间:2020年1月1日 农历腊月初七 星期三 目前距离2020年元旦还有 127 天 10 时 24 分 8 秒 [removed] function getRTime() { var EndTime = new Date("2020/01/01 00:00:00"); //截止时间 var NowTime = new Date(); var t = EndTime.getTime() - NowTime.getTime(); var d = Math.floor(t / 1000 / 60 / 60 / 24); var h = Math.floor(t / 1000 / 60 / 60 % 24); var m = Math.floor(t / 1000 / 60 % 60); var s = Math.floor(t / 1000 % 60); document.getElementById("t_d")[removed] = d + " 天"; document.getElementById("t_h")[removed] = h + " 时"; document.getElementById("t_m")[removed] = m + " 分"; document.getElementById("t_s")[removed] = s + " 秒"; } setInterval(getRTime, 1000); [removed] </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值