




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
学习angular ui-router - 管理状态,有需要的朋友可以参考下。参考原文:/angular-ui/ui-router/wikiui-router 的工作原理非常类似于 Angular 的路由控制器,但它只关注状态。 在应用程序的整个用户界面和导航中,一个状态对应于一个页面位置 通过定义controller、template和view等属性,来定义指定位置的用户界面和界面行为 通过嵌套的方式来解决页面中的一些重复出现的部位最简单的形式模板可以通过下面这种最简单的方式来指定1234 1234/ in app-states.js (or whatever you want to name it)$stateProvider.state(contacts, template: My Contacts)模板将被插入哪里?状态被激活时,它的模板会自动插入到父状态对应的模板中包含ui-view属性的元素内部。如果是顶层的状态,那么它的父模板就是index.html。激活状态有三种方法来激活状态:1. 调用$state.go()方法,这是一个高级的便利方法;2. 点击包含ui-sref指令的链接;3. 导航到与状态相关联的 url。Templates 模板可以通过下面几种方法来配置一个状态的模板。方法一:配置template属性,指定一段 HTML 字符串,这人是设置模板的最简单的方式123$stateProvider.state(contacts, template: My Contacts)方法二:配置templateUrl属性,来加载指定位置的模板,这是设置模板的常用方法。123$stateProvider.state(contacts, templateUrl: contacts.html)templateUrl的值也可以是一个函数返回的url,函数带一个预设参数stateParams。12345$stateProvider.state(contacts, templateUrl: function (stateParams) return /partials/contacts. + stateParams.filterBy + .html; )方法三:通过templateProvider函数返回模板的 HTML。1234567$stateProvider.state(contacts, templateProvider: function ($timeout, $stateParams) return $timeout(function () return + $stateParams.contactId + , 100); )如果想在状态被激活前,让有一些默认的内容,当状态被激活之后默认内容将被状态对应的模板替换。12345 Some content will load here! Controllers控制器可以为模板指定一个控制器(controller)。警告:控制器不会被实例化如果模板没有定义。设置控制器:123456$stateProvider.state(contacts, template: 学习 ui-router - 管理状态, controller: function($scope) $scope.title = My Contacts; )如果在模块中已经定义了一个控制器,只需要指定控制器的名称即可:1234$stateProvider.state(contacts, template: ., controller: ContactsCtrl)使用controllerAs语法:1234$stateProvider.state(contacts, template: ., controller: ContactsCtrl as contact)对于更高级的需要,可以使用controllerProvider来动态返回一个控制器函数或字符串:1234567$stateProvider.state(contacts, template: ., controllerProvider: function($stateParams) var ctrlName = $stateParams.type + Controller; return ctrlName; )控制器可以使用$scope.on()方法来监听事件状态转换。控制器可以根据需要实例化,当相应的scope被创建时。例如,当用户点击一个url手动导航一个状态时,$stateProvider将加载对应的模板到视图中,并且将控制器和模板的scope绑定在一起。解决器 Resolve可以使用resolve为控制器提供可选的依赖注入项。resolve配置项是一个由key/value构成的对象。 key string:注入控制器的依赖项名称。 factory - string|function:o string:一个服务的别名o function:函数的返回值将作为依赖注入项,如果函数是一个耗时的操作,那么控制器必须等待该函数执行完成(be resolved)才会被实例化。例子在controller实例化之前,resolve中的每一个对象都必须 be resolved,请注意每个 resolved object 是怎样作为参数注入到控制器中的。123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566$stateProvider.state(myState, resolve: / Example using function with simple return value. / Since its not a promise, it resolves immediately. simpleObj: function() return value: simple!; , / Example using function with returned promise. / 这是一种典型使用方式 / 请给函数注入任何想要的服务依赖,例如 $http promiseObj: function($http) / $http returns a promise for the url data return $http(method: GET, url: /someUrl); , / Another promise example. / 如果想对返回结果进行处理, 可以使用 .then 方法 / 这是另一种典型使用方式 promiseObj2: function($http) return $http(method: GET, url: /someUrl) .then (function (data) return doSomeStuffFirst(data); ); , / 使用服务名的例子,这将在模块中查找名称为 translations 的服务,并返回该服务 / Note: The service could return a promise and / it would work just like the example above translations: translations, / 将服务模块作为解决函数的依赖项,通过参数传入 / 提示:依赖项 $stateParams 代表 url 中的参数 translations2: function(translations, $stateParams) / Assume that getLang is a service method / that uses $http to fetch some translations. / Also assume our url was /:lang/home. translations.getLang($stateParams.lang); , / Example showing returning of custom made promise greeting: function($q, $timeout) var deferred = $q.defer(); $timeout(function() deferred.resolve(Hello!); , 1000); return mise; , / 控制器将等待上面的解决项都被解决后才被实例化 controller: function($scope, simpleObj, promiseObj, promiseObj2, translations, translations2, greeting) $scope.simple = simpleObj.value; / 这里可以放心使用 promiseObj 中的对象 $scope.items = promiseObj.items; $scope.items = promiseObj2.items; $scope.title = translations.getLang(english).title; $scope.title = translations2.title; $scope.greeting = greeting; )为 $state 对象提供自定义数据可以给 $state 对象提供自定义数据(建议使用data属性,以免冲突)123456789101112131415161718/ 基于对象和基于字符串定义 state 的例子var contacts = name: contacts, templateUrl: contacts.html, data: customData1: 5, customData2: blue $stateProvider .state(contacts) .state(contacts.list, templateUrl: contacts.list.html, data: customData1: 44, customData2: red )可以通过下面的方式来访问上面定义的数据:1234function Ctrl($state) console.log($state.current.data.customData1) / 输出 5; console.log($state.current.data.customData2) / 输出 blue;onEnter 和 onExit 回调函数onEnter和onExit回调函数是可选配置项,分别称为当一个状态变得活跃的和不活跃的时候触发。回调函数也可以访问所有解决依赖项。123456789101112131415$stateProvider.state(contacts, template: 学习 ui-router - 管理状态, resolve: title: My Contacts , controller: function($scope, title) $scope.title = My Contacts; , / title 是解决依赖项,这里也是可以使用解决依赖项的 onEnter: function(title) if(title) . do something . , / title 是解决依赖项,这里也是可以使用解决依赖项的 onExit: function(title) if(title) . do something . )State Change Events 状态改变事件所有这些事件都是在$rootScope作用域触发 $stateChangeStart- 当模板开始解析之前触发12$rootScope.$on($stateChangeStart, function(event, toState, toParams, fromState, fromParams) . )注意:使用event.preventDefault()可以阻止模板解析的发生123456$rootScope.$on($stateChangeStart, function(event, toState, toParams, fromState, fromParams) event.preventDefault(); / transitionTo() promise will be rejected with / a transition prevented error) $stateNotFound-v0.3.0- 在 transition 时通过状态名查找状态,当状态无法找到时发生。该事件在 scope 链上广播,只允许一次处理错误的机会。unfoundState将作为参数传入事件监听函数,下面例子中可以看到unfoundState的三个属性。使用event.preventDefault()来阻止模板解析,12345678910/ somewhere, assume lazy.state has not been defined$state.go(lazy.state, a:1, b:2, inherit:false);/ somewhere else$scope.$on($stateNotFound, function(event, unfoundState, fromState, fromParams) console.log(unfoundState.to); / lazy.state console.log(unfoundState.toParams); / a:1, b:2 console.log(unfoundState.options); / inherit:false + default options) $stateChangeSucces
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年应急发电保障员笔试预测题
- 司炉工安全考试题及答案
- 顺丰仓储考试题库及答案
- 2025年养老驿站护理员面试模拟题集
- 省公务员考试题及答案
- 上市证券面试题目及答案
- 陕西省建筑a证考试题库及答案
- 陕西生物会考试题及答案
- 山西五省联考试题及答案
- 2025年物流末端派送服务合同
- 中华人民共和国史第一章中华人民共和国的诞生和社会主义制度的确立课件
- GB/T 23483-2009建筑物围护结构传热系数及采暖供热量检测方法
- GB/T 22237-2008表面活性剂表面张力的测定
- 股指期权风险管理
- 常用急救药品的剂量与用法课件
- 《电业安全工作规程》
- 塔吊基础-专项施工方案
- 发证机关所在地区代码表
- 过去分词公开课--完整版PPT课件
- 书法的章法布局(完整版)
- GB∕T 10429-2021 单级向心涡轮液力变矩器 型式和基本参数
评论
0/150
提交评论