首先需要下载最新的angular文件,这里有一个快捷的方式:npm install angular@1.3,我这里是下载最新的1.3版本。
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部分
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的版本的,老版本写法会不一样的,否则会有吭的