Angular控制器

這裏使用的是angular-1.0.1.min.jsjavascript

Angular的前端渲染html

<div>
	<ul>
		<li ng-repeat="i in [1,2,3]">
			<h1>{{i}}</h1>
		</li>
	</ul>
</div>

效果:前端

使用控制器讀取JSON:java

<div ng-controller="PhoneListCtrl">
	<ul>
		<li ng-repeat="phone in phones">
			<h1>{{phone.name}}</h1>
			<p>{{phone.snippet}}</p>
		</li>
	</ul>
</div>

控制器代碼:測試

function PhoneListCtrl($scope) {
	$scope.phones = [{
			"name" : "Nexus S",
			"snippet" : "Fast just got faster with Nexus S."
		}, {
			"name" : "Motorola XOOM with Wi-Fi",
			"snippet" : "The Next, Next Generation tablet."
		}, {
			"name" : "MOTOROLA XOOM",
			"snippet" : "The Next, Next Generation tablet."
		}
	];
}

效果:3d

 

你也能夠本身定義一個控制器:htm

<div ng-controller="mycontroller">
	<p>{{ article.ID }}</p>
	<p>{{ article.Name }}</p>
	<button ng-click="func()">測試</button>
</div>

控制器實現:blog

function mycontroller($scope) {
	$scope.article = {
		ID : 1,
		Name : "hell world"
	};
	$scope.func = function () {
		alert(1);
	}
}

效果:ip

相關文章
相關標籤/搜索