angular中定義全域性變數及全域性變數的使用

魔豆發表於2016-09-12

一個例子,定義了兩個變數,並且把變數顯示出來:

<!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>

 

相關文章