码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS 父子控制器

时间:2015-06-13 23:02:21      阅读:829      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html ng-app="myApp">
	<head>
		<script src="C:\\Users\\yuli\\Desktop\\bower_components\\angularjs\\angular.js"></script>
		<script src="C:\\Users\\yuli\\Desktop\\bower_components\\jquery\\dist\\jquery.js"></script>
		<script type="text/javascript">
			var myModule = angular.module(‘myApp‘, []);
			myModule.controller(‘MyController‘, function ($scope, $timeout) {
				var updateClock = function() {
					$scope.clock = new Date();
					$timeout(function() {
						updateClock();
					}, 1000);
				};
				updateClock();

				$scope.a_click = function (msg){
					console.log(msg);
				}
				$scope.value = 1;
				$scope.add = function (n){
					$scope.value += n;
				}

				$scope.clear = function(){
					$scope.value = 0;
				};
			});
			myModule.controller(‘ChildController‘, function ($scope) {
				$scope.add_child = function (n){
					$scope.value += n;
				}
			});
			myModule.run(function($rootScope){
				$rootScope.rootValue = -1;
				$rootScope.add_n = function(n){
					$rootScope.rootValue += n;
				};
			});
		</script>
	</head>
	<body>
		<div ng-controller="MyController">
			<h1>Hello {{ clock }}!</h1>
			<a href="" ng-click="a_click(‘hello‘)">click me console.log message.</a>
			<a href="" ng-click="add(2)">add 2.</a>
			<input type="text" ng-model="value"/>
			<div ng-controller="ChildController">
				<a href="" ng-click="add_child(2)">add 2.</a>
				<input type="text" ng-model="value"/>
			</div>

			<br />
			<a href="" ng-click="clear()">clear.</a>
			<br />
			<input type="text" ng-model="rootValue"/>
			<a href="" ng-click="add_n(value)">add n.</a>
		</div>
	</body>
</html

  

AngularJS 父子控制器

标签:

原文地址:http://www.cnblogs.com/chengshuiqiang/p/4574220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!