[AngularJS] Consistency between ui-router states and Angular directives
ui-router's states and AngularJS directives have much in common. Let's explores the similarities between the two and how these patterns have emerged in Angular. Keeping your states and directives consistent can also help with refactoring as your app grows larger over time.
angular.module('app', ['ui.router', 'ui.bootstrap'])
.config('home', function($stateProvider){
$stateProvider.state('home', {
url: "",
controller: "HomeController as homeCtrl"
templateUrl: "templates/home.tpl.html"
})
})
.controller("HomeController", function(){
var homeCtrl = this;
homeCtrl.content = "Content from the controller";
})
.directive('appHeader', function(){
return{
controller: "AppHeaderController as headerCtrl",
templateUrl: "templates/appHeader.tpl.html"
}
})
.controller('AppHeaderController', function(){
var headerCtrl = this;
headerCtrl.content = "This content if from header";
});
What we can see now is that our controllers are pretty much exactly the same in the way that we set them up and configure them. Our directive configuration object here is pretty much the same as our state provider configuration object here.
One difference that you can notice is that we do have a function here wrapping around this return statement. Now that using a controller is more common place this could actually go away, but I don't see that happening anytime soon.
The other idea or concept which is very similar is using state params to pass in data from the URL and scope to pass in data through attributes on the element. The pattern that we've really seen emerge here is defining a configuration for your state from the state provider, having a controller handle all of the code and the APIs for the different services you're going to access, and then just dropping everything in your template to design what that state looks like.
Those exact same ideas apply to directives as well, where this is your configuration on how to set up your directive, this is all of your code and APIs to access services, and this is just your template. What this means is that if you have a directive that you think is getting too big or too much for just a component, it could easily evolve and grow into a state. Or if you have a state which you think is too small for taking up an entire view, you could shrink that down into a directive component.
See more: https://egghead.io/lessons/angularjs-consistency-between-ui-router-states-and-angular-directives
[AngularJS] Consistency between ui-router states and Angular directives的更多相关文章
- angularjs ngRoute和ui.router对比
ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...
- angularjs的路由ui.router
<!-- 引入路由插件 --> <script src="vendor/angular-ui-router/release/angular-ui-router.min. ...
- AngularJS学习之 ui router
1.安装 bower install --save angular_ui-router 2.在项目主页面 index.html中添加 <div ui-view="">& ...
- AngularJS 使用 UI Router 实现表单向导
Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...
- [转]AngularJS 使用 UI Router 实现表单向导
本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...
- [转]AngularJS+UI Router(1) 多步表单
本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现 在线demo演示地址https://rawgit.com/dream ...
- angular 的ui.router 定义不同的state 对应相同的url
Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...
- angular : $location & $state(UI router)的关系
次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...
- angular ui.router 路由传参数
angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...
随机推荐
- Linux iostat监测IO状态
Linux iostat监测IO状态 http://www.orczhou.com/index.php/2010/03/iostat-detail/
- setImageResource和setImageDrawable区别
ImageView设置图片的方式有很多钟,可以在xml里面写android:src=”@drawable/xxx”,也可以在java代码里面设置. 在java里面的设置方式也有多种,方法包括:setI ...
- The Bookcase
题意: 有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的) 分析: 不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度 dp[i][j]= ...
- XTUOJ1247 Pair-Pair 预处理+暴力
分析:开个1000*1000的数组,预处理矩阵和,然后分类讨论就好 时间复杂度:O(n) #include <cstdio> #include <iostream> #incl ...
- STM32 TIM重映射
复用功能 没有重映射 部分重映射 完全重映射 TIM3_CH1 PA6 PB4 PC6 CH2 PA7 PB5 PC7 CH3 PB0 PB0 PC8 CH4 PB1 PB1 PC9 /**重映射 t ...
- qt文本编辑器
示例代码: mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include ...
- Web服务器(Apache)虚拟主机的配置
一.定义 所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务. 二.虚拟主机的实现方法有三种: 基于IP的方法,基于主机名的方法和基于端口的法官法. ...
- QT-【转】基础(略)
第0篇 开始学习Qt 与Qt Creator 第1篇 基础(一)Qt开发环境的搭建和hello world 第2篇 基础(二)编写Qt多窗口程序 第3篇 基础(三)Qt登录对话框 第4篇 基础(四)添 ...
- HDFS分布式文件系统设计思想
HDFS设计目标 1)硬件错误是常态,数据保存需要冗余. 2)数据批量读取,Hadoop擅长数据分析而不是事务处理. 3)大规模数据集. 4)简单一致醒模型,降低系统复杂度,文件一次写入多次读取, 5 ...
- build.gradle 使用tips
7.查看依赖 gradlew [你想查看的module]:dependencies >dependencies.txt 6.buildToolsVersion build tools版本号 co ...