博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS] Consistency between ui-router states and Angular directives
阅读量:7284 次
发布时间:2019-06-30

本文共 2498 字,大约阅读时间需要 8 分钟。

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: 

转载地址:http://cmzjm.baihongyu.com/

你可能感兴趣的文章
移动APP开发中8大安全问题
查看>>
ElementUI中tree控件踩坑记
查看>>
自定义jquery插件
查看>>
游戏AI(二)—行为树优化之内存优化
查看>>
Mozilla网站安全分析工具Observatory已发布
查看>>
.NET Core 2.1改进了性能,并提供了新的部署选项
查看>>
AWS推出RoboMaker,可构建智能机器人应用程序
查看>>
1100名达摩院“扫地僧”加持,阿里云的下一个十年
查看>>
取代ZooKeeper!高并发下的分布式一致性开源组件StateSynchronizer
查看>>
《Elasticsearch in Action》书评与作者访谈
查看>>
腾讯云发布CDN 2.0,四大优势加速移动化进程
查看>>
Visual Studio 2017 15.6预览版最新进展
查看>>
ZenHub Epics创造了GitHub中敏捷Epics
查看>>
《七周七并发模型》作者Paul Butcher、阿里云研究员余锋(褚霸)——QCon北京2016前瞻...
查看>>
iOS应用开发登陆Windows平台惹争议
查看>>
《Java 20年:道路与梦想》迷你书发布
查看>>
GitHub的MySQL高可用性实践
查看>>
微软发布Azure Application Insights for Node.js 1.0版本
查看>>
UPYUN亮相GIF2016 展示实时性能监控系统
查看>>
Node.js async.parallelLimit 与 async.eachLimit 的区别与不同使用场景
查看>>