<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<script src="lib/angular.min.js"></script> | |
<script> | |
var app=angular.module("gaoynApp",[]); | |
//控制器1 $scope作用域:只在该控制器内; $rootScope 在一个模块内 | |
app.controller("demo1",["$scope","$rootScope",function($scope,$rootScope){ | |
$rootScope.name="我是全局变量"; | |
}]); | |
app.controller("demo2",["$scope","$rootScope",function($scope,$rootScope){ | |
$scope.username=$rootScope.name; | |
}]); | |
</script> | |
</head> | |
<body ng-app="gaoynApp"> | |
<!--在控制器demo1中调用 name变量 --> | |
<ul ng-controller="demo1"> | |
<li>{{name}}</li> | |
</ul> | |
<!--在控制器demo2中调用 全局name变量--> | |
<ul ng-controller="demo2"> | |
<li>{{name}}</li> <!--demo2中是否有name?--> | |
<li>{{username}}</li> <!--demo2中是否有name?--> | |
</ul> | |
</body> | |
</html> |
angularJs作用域
最新推荐文章于 2019-11-27 13:38:56 发布