- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <script src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular.min.js" type="text/javascript"></script>
- <script>
- var myApp = angular.module('myApp', []);
- //方法1 value
- myApp.value("public",{
- "Localapi":"/v1/getInfo/",
- "Versions":"V2.2.3"
- });
- //方法2 constant
- myApp.constant("option", function(){
- return {
- //全局共用函数
- fn : function(str) {
- return "text"
- },
- //变量
- Localapi : "/v1/getInfo/",
- //数组
- array : [200,300,500,600]
- }
- });
- //使用,注入public
- myApp.controller("Demo",["$scope","public",function($scope,public){
- $scope.public = public;
- }])
- //使用,注入public
- myApp.controller("Demo2",["$scope","option",function($scope,option){
- $scope.option = option();
- }])
- </script>
- </head>
- <body ng-app="myApp">
- <div ng-controller="Demo">
- {{ public.Localapi }}
- </div>
- <div ng-controller="Demo2">
- {{ option.Localapi }} <br/>
- <span ng-repeat="(key, value) in option.array">
- ({{ key }} -- {{ value }})
- </span>
- </div>
- </body>
- </html>
Ag设置全局变量的N种方法
最新推荐文章于 2023-03-21 17:03:06 发布