|
AngularJS支持模塊化方法。模塊用于單獨(dú)的邏輯表示服務(wù),控制器,應(yīng)用程序等。為保持代碼簡(jiǎn)潔,我們?cè)趩为?dú)的 js 文件中定義模塊,并將其命名為 module.js文件。 在這個(gè)例子中,我們要?jiǎng)?chuàng)建兩個(gè)模塊。 應(yīng)用模塊 - 控制器用于初始化應(yīng)用程序。 控制器模塊 - 用于定義控制器。 應(yīng)用模塊mainApp.js var mainApp = angular.module("mainApp", []);12復(fù)制代碼類型:[html]在這里,我們聲明了使用 angular.module 函數(shù)的應(yīng)用程序mainApp模塊。我們已經(jīng)傳遞一個(gè)空數(shù)組給它。這個(gè)數(shù)組通常包含依賴模塊。 控制器模塊mainApp.controller("studentController", function($scope) {
$scope.student = {
firstName: "Mahesh",
lastName: "Parashar",
fees:500,
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65},
{name:'English',marks:75},
{name:'Hindi',marks:67}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});1234567891011121314151617181920復(fù)制代碼類型:[html]在這里,我們聲明了使用 mainApp.controller 函數(shù)的一個(gè)控制器 studentController 模塊。 使用模塊<div ng-app="mainApp" ng-controller="studentController">..<script src="mainApp.js"></script><script src="studentController.js"></script>123456復(fù)制代碼類型:[html] 這里我們使用 ng-app 指令和控制器使用 ng-controller 指令的應(yīng)用模塊。我們已經(jīng)在主HTML頁(yè)面導(dǎo)入 mainApp.js 和 studentController.js。 |
|
|
來(lái)自: 碼農(nóng)9527 > 《Java》