首先須要下載最新的angular文件,這裏有一個快捷的方式:npm install angular@1.3,我這裏是下載最新的1.3版本。javascript
html 部分html
<!DOCTYPE html> <html ng-app="app"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div ng-controller="CommonController"> <div ng-controller="Controller1"> <p>{{greeting.text}},Angular</p> <button ng-click="test1()">test1</button> </div> <div ng-controller="Controller2"> <p>{{greeting.text}},Angular</p> <button ng-click="test2()">test2</button> </div> <button ng-click="commonFn()">通用</button> </div> </body> <script type="text/javascript" src="scripts/lib/angular.js"></script> <script type="text/javascript" src="scripts/app/mvc.js"></script> </html>
js部分java
var app = angular.module("app",[]); app.controller("CommonController",function($scope){ $scope.commonFn = function(){ alert("這是通用功能"); }; }) app.controller("Controller1",function($scope){ $scope.greeting = { text:'hello' } $scope.test1 = function(){ alert("test1") } }) app.controller("Controller2",function($scope){ $scope.greeting = { text:'hello' } $scope.test2 = function(){ alert("test2") } })
這裏的寫法是1.3的版本的,老版本寫法會不同的,不然會有吭的npm