国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解AngularJS 模塊化

2024-05-06 15:15:53
字體:
來源:轉載
供稿:網友

學習要點:

    控制器模塊化 指令模塊化 過濾器模塊化 服務模塊化 定義值模塊化 使用模塊工作

第一步:創建一個模塊

// function : define module named exampleApp// param detail :// param one : module name// param two : relay on modules collection// parms three : config informationvar myApp = angular.module("exampleApp", ["exampleApp.Controllers", ["exampleApp.Controllers", "exampleApp.Filters", "exampleApp.Directives", "exampleApp.Service", "exampleApp.Values"])

在視圖中應用模塊

<!-- use module --><html ng-app="exampleApp"> ...</html>

第二步:定義值

var valueModule = angular.module("exampleApp.Values", [])// defind valuevar now = new Date();valueModule.value("nowValue", now);

第三步:定義服務

var serviceModule = angular.module("exampleApp.Service", [])// function : define a service named daysserviceModule.service("days", function (nowValue) {  this.today = nowValue.getDay();  this.tomorrow = this.today + 1; })

第四步:定義控制器

var controllerModule = angular.module("exampleApp.Controllers", []);// function : define a controller named dayCtrl// the controller include two param:// param detail:// param one : name of controller// param two : a factory function // the param $scope of factory function show information to viewcontrollerModule.controller("dayCtrl", function ($scope, days) {   // days : use custom service // today is ... $scope.day = days.today; // tomorrow is ... $scope.tomorrow = 7;})

將控制器應用于視圖

<!-- use controller --> <div class="panel" ng-controller="dayCtrl">  <div class="panel-header">   <h3>Angular App</h3>  </div>  <!-- if the day is undefined, show unknow -->  <!-- use filter and data binding -->  <h4>Today is {{ day || "unknow" }}</h4>  <h4>Tomorrow is {{ tomorrow || "unknow" }}</h4> </div>

第五步:定義指令

var directiveModule = angular.module("exampleApp.Directives", []);// function : define a directive named highlight// it accepts two param// param one : the name of directive // param two : a factory methoddirectiveModule.directive("highlight", function ($filter) {  // get the filter function  var dayFilter = $filter("dayName");  // param detail:  // scope : view scope of action  // element : the element which uses the custom directive  // attrs : the attrs of the element  return function (scope, element, attrs) {   // console.log(dayFilter(scope.day));   if (dayFilter(scope.day) == attrs['highlight']) {    element.css("color", 'red');   }  } })

將指令應用于視圖

...<h4 highlight="Saturday">Today is {{ day || "unknow" | dayName }}</h4>...            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 揭阳市| 汶川县| 应城市| 台南县| 松江区| 香港| 汝州市| 抚顺县| 梁平县| 仲巴县| 和平县| 六枝特区| 房山区| 偃师市| 寿阳县| 青龙| 阳西县| 宁城县| 临安市| 蒙城县| 大庆市| 清涧县| 山西省| 金溪县| 名山县| 彰化市| 梅州市| 伊宁县| 白山市| 永川市| 施秉县| 基隆市| 徐水县| 宁国市| 班玛县| 中牟县| 乌兰察布市| 乐山市| 泸水县| 尼勒克县| 固阳县|