angular中定义全局变量及全局变量的使用

时间:2023-02-12 08:45:47

一个例子,定义了两个变量,并且把变量显示出来:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>interval</title>
<script type="text/javascript" src="js/angular/angular.min.js" charset="utf-8"></script>
</head>
<body ng-controller="ctrl">
<div>{{text1}}</div>
<div>{{text2}}</div>
<script>
var app = angular.module('myApp',[]);

app.value("obj1", "aaa");
app.value("obj2",{a1:1,a2:'abc'});

app.controller("ctrl",["$scope","obj1","obj2",function($scope,obj1,obj2){

    
    $scope.text1 = obj1;
    $scope.text2 = obj2.a1 + ":" + obj2.a2;
}]);
</script>
</body>
</html>