1. 程式人生 > >angular中 value和constant

angular中 value和constant

value也是angular.Module中的方法value(name, object);其中name是service的名稱,object是伺服器例項物件,這個時候我們就可以把上邊的程式碼修改正成這樣

<!doctype html>
<html>
	<head>
		<script src="angular.min.js" type="text/javascript"></script>
		<script type="text/javascript">
		var app = angular.module('myapp',[])
		.value('testvalue','word');
		app.controller('mytest',function($scope,testvalue){
			$scope.test="hello "+ testvalue;
		});
		</script>
		<title>learing argularjs--widuu</title>
	</head>
	<body ng-app='myapp' ng-controller='mytest' >
	<input ng-model='test'>{{test}}
	</body>
</html>

constant也是angular.Module中的方法constant(name, object);其中name是常量的名稱,而object是常量的值

<!doctype html>
<html>
	<head>
		<script src="angular.min.js" type="text/javascript"></script>
		<script type="text/javascript">
		var app = angular.module('myapp',[])
			.value('testvalue','widuu')
			.constant('count',23)
			.service('testservice',
				function(testvalue,count){
					this.lable = function(){
						return "this will output:hello "+testvalue+",age is "+count;
					}
				}
			);
		app.controller('mytest',function($scope,testvalue,testservice){
			$scope.test = "hello "+ testvalue;
			$scope.output = testservice.lable();
		});
		</script>
		<title>learing argularjs--widuu</title>
	</head>
	<body ng-app='myapp' ng-controller='mytest' >
	<input ng-model='test'>{{test}}
	</p>
		{{output}}
	</body>
</html>