技术控

    今日:25| 主题:49195
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Does Angular 2 live up to the hype?

[复制链接]
梦里见! 发表于 2016-10-4 01:16:30
133 6

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
By Tyler Church, Architect and Director of Development
  The hype is alive and well:
   “Angular 2 is not just another framework”
   “Angular 2 is easier”
   “Angular 2 is the future”
  And the cynics are kicking just as hard:
   “Learning Angular 2 was excruciating”
   “Angular 2 is too little too late”
   ”Angular 2 isn’t worth it right now”
   As we watch Angular 2’s first production release version roll-out , many of us are asking ourselves these questions: Who do I believe? Just what is in Angular 2? Is it really that much better than Angular 1? Do I upgrade now or do I continue on with my existing framework, especially if that is Angular 1?
  We took the dive into Angular 2 – Here’s how it went!

   When Angular 2 went into beta, we began experimenting with it. And (spoiler alert!) Angular 2 became the basis for our newly releasedCycligent Git Tool, a free graphical Git tool that makes Git much easier to understand and use.
  So what do you need to know about Angular 2?
   
Does Angular 2 live up to the hype?-1 (especially,framework,continue,existing,believe)

   The first thing to know about Angular 2 is that it’s different from Angular 1. We mean really different. As in its better to forget what you know about Angular 1 then to try and apply it to Angular 2. There are a lot of things with the same names (though even more things with different names), but don’t let that lull you into trying to do things in the same way as you did in Angular 1. Core concepts have changed. You’re not in Kansas anymore.
  Our recommended approach is to take the general sensibilities you’ve gained about building single-page apps with you, but leave everything else you know about Angular 1 behind. If you try to make Angular 2 work like Angular 1, you’ll just end up causing yourself large amounts of unnecessary grief.
  So what is Angular 2 going to give me?

  Before getting into the specifics, I think generally what you’ll find with Angular 2 is that everything has been very well thought out. Angular 1 was also the product of some deep thinking about web apps (which is one of the reasons I think it was so successful), but you can tell with Angular 2 that they’ve gone even farther, removing many of the warts that Angular 1 had.
  Here’s what’s new and great:
  
       
  • Similar to directives in Angular 1 (but don’t take the similarity too far, they’re different!), they’ve become much more flexible and easier to think about, and are a breath of fresh air compared to what came before. Some of the new niceties of Components:
           
    • Each component is a class that stands on its own. This makes them very easy to read, very easy to test, and very easy to separate them out into separate files.     
    • Components no longer pollute a module namespace. In Angular 1 it was very easy to be lazy and throw many or all of your components into one module, and even if you properly separated them, it was difficult to understand the dependency chain that you created. In Angular 2, you list out the Components that you are using in the places where you are using them, so it’s very easy to tell what needs to load what. And if you’re using TypeScript, missing dependencies will become compile errors.     
    • With constructs like ViewChildren, Angular 2 will automatically keep a list up-to-date with all the child Components. No more querying around calling .controller() or .scope()     
    • Other decorators and lifecycle hooks make it very easy to write code that receives certain data and handles various events.   
       
  • Better events and outputs. Angular 2 changes the way events are handled. The new name of the game is: If there’s an event, you can bind to it. So say goodbye to ngClick, ngKeypress, and friends. Just grab the DOM events you need by their normal names. Angular 2 also makes it drop-dead simple for your Components to emit their own events and other data, and it follows the same conventions as the built-in DOM events, which makes it very easy to use.   
  • No more $scope.$apply(); In Angular 1, if you wanted to integrate with any non-Angular JavaScript code, you were forever making calls to $scope.$apply() or $scope.$digest(). Even with lots of practice using Angular 1, I would still find myself confused in situations where I’d go “Why didn’t the UI update!?” only to finally realize that I had called out to a non-Angular function, and Angular had no idea anything had changed. Angular 2 adds hooks to all the various async JavaScript operations you could do, and therefore everything generally “just works”. After being used to Angular 1, I was skeptical about this at first, but it works well!   
  • No more $scope at all! It was never abundantly clear in Angular 1 when you should put a property on $scope, or a property on a directive, or a property on a controller, or some other place. Now $scope is gone, and you simply add properties to your Component classes as they make sense in the design of your class.   
  • Easily share data between Components. In Angular 1 if you needed to share some data between directives you had a weird dilemma: Do I use a Provider? A Factory? A Service? A Value? A Constant Value? It seemed like I always had to reread the docs to figure it out. In Angular 2, you make a class that contains the data, you decorate it with @Injectable(). Done.   
  • Worry less about stylesheets. By default, styles used on a Component are scoped to a particular Component, so you don’t have to worry about styles on some special Component you’ve built trashing the styles on all inputs on the page.   
  • Feel good about accessing the DOM. Angular 2 is no longer trying to be jQuery by including their own “jqlite” module. We could argue about whether this is good or bad, but I think it’s good. Rather than wondering where Angular ends, and DOM manipulation begins, it’s often much more clear in Angular 2 where Angular isn’t going to help you; indicating that you should reach out to a library like jQuery.  
   This is just scratching the surface, and there’s a lot more to Angular 2. If I had to pick one thing I’d say: I’m just a lot happier working in Angular 2. Angular 1 made web app development a much happier experience, Angular 2 takes that even further.
  TypeScript vs JavaScript

Does Angular 2 live up to the hype?-2 (especially,framework,continue,existing,believe)

  One of the big things you’ll notice about Angular 2 is that they really push you to use TypeScript.
  We’ve had a long history building things in JavaScript, and technologies like Angular 1 and Node.js pushed us even further into our love for JavaScript. So at first the idea of using JavaScript’s strange cousin TypeScript seemed a bit repugnant to us. One of our favorite aspects of JavaScript was that we didn’t need a compile step like our .NET and Java brethren: Just hit refresh! TypeScript required a compile step so we tried to avoid it for a long time.
  Support for plain JavaScript in Angular 2 was severely lacking in the beta stages. It may have been possible, but it was very poorly documented. We fought with it for a long time, and eventually acquiesced to use TypeScript. And oh how happy we were when we did!
  Some of the things we now love about TypeScript:
  
       
  • Use as much or as little of the typing system you want! Since TypeScript is just a superset of JavaScript, you can write much of your code as you normally would, cranking up the typings wherever you feel it makes the most sense. After a period of getting used to things, we settled on using typings much more often than we didn’t. Though TypeScript’s “any” type provides a lovely escape hatch that we occasionally use when we’re writing very dynamic code.   
  • Interfaces make building a large app worlds easier. In JavaScript, passing around custom data structures is easy, but it’s not always easy to document what those structures need to contain. With TypeScript, it’s a no-brainer, just create an interface. We’ve gone so far as to write a helper script that parses our backend code and generates an API file with all the proper typings for our frontend. So if a backend function changes what it’s expecting or returning, we immediately start getting TypeScript errors.   
  • Refactoring is now a breeze. Refactoring with JavaScript always consisted of a lot of CTRL+F-ing around the codebase and saying a few prayers that I didn’t miss some piece of code. With TypeScript I’ll often refactor by just deleting or renaming functions wholesale, confident that the compiler will tell me everywhere that I need to fix.   
  • Classes are first-class citizens. We love classes, so much so that the primary client-side JavaScript library we developed includes helpers for building classes more easily (as well as lots of other goodies, like handling async script loading and – gasp! – Type checking on function arguments! I guess we were on the path to TypeScript all along).   
  • Use ES6. Since TypeScript can compile to older JS versions, many ES6 features that aren’t yet widely implemented in browsers are usable today.   
  • Language parsing tools. If you want to write any helper scripts for working with your code, or otherwise analyze the TypeScript code you have our there, the TypeScript compiler provides an API that makes it very easy to do so. It’ll take a little bit to get your bearings, but once you do it’ll be smooth sailing.  
   So overall the conclusion about TypeScript for us is clear: We are now enamored with TypeScript! Whether or not you take the plunge for Angular 2, your JavaScript development will be greatly improved by switching to TypeScript.
  Angular 2: Now or Later?

   Since so much has changed in Angular 2, many people have expressed frustration with updating their apps. Angular 2 offers an upgrade path from Angular 1 ,  but I can’t vouch for it directly. From a technical perspective, we think Angular 2 is leagues better than Angular 1, and so for all new projects we are starting with Angular 2.
  For an existing project, whether to switch from Angular 1 to Angular 2 is a decision where you need to weigh business needs and other considerations specific to your project very heavily when considering a switch. Long-term, I think Angular 2 and TypeScript will yield productivity and happiness improvements for your developers, which should result in more business value being delivered over the long term. But rewriting an app is expensive, so the most worthwhile path would probably be to slowly convert say one page at a time over to Angular 2.
  Angular 2 Tips and Tricks

  So if you’re like us and think these benefits of Angular 2 and TypeScript make it worth jumping into Angular 2, here’s a few tips and tricks that we hope you’ll find useful:
  
       
  • I said it once and I’ll say it again: Forget everything you know about Angular 1. Don’t inflict upon yourself the pain of trying to use Angular 2 the way you used Angular 1. Some concepts have the same names, but your default mindset should be that you assume everything is different and you need to read up on it.   
  • Templates are different now and you should make sure you’ve read this: https://angular.io/docs/ts/latest/guide/template-syntax.html   
  • Use Angular only when it helps you , and reach for jQuery or direct DOM manipulation when it doesn’t. For example, we had a custom list view that could contain thousands and thousands of elements, with actions available on each one. Angular would try to reprocess all those items fairly often, and was generally unhelpful in making it easy for the user to perform actions on items in the list. With direct DOM manipulation, the solution was much less frustrating to implement not to mention worlds faster. It can be far too easy to tie yourself up in trying to have Angular do everything for you all the time. Don’t be afraid to reach out to the DOM directly when you need to.   
  • Prefer TypeScript over JavaScript. You might be nervous to make the jump like we were, but I think if you give it a shot you won’t look back.   
  • Avoid impure pipes. They cause Angular to re-evaluate a template as often as it can, which can slow your app down a lot, especially if the pipe is doing something computationally expensive.   
  • Avoid calling functions in your templates to set attributes. It’s tempting to write something like this: Does Angular 2 live up to the hype?-3 (especially,framework,continue,existing,believe)
    Don’t do it! Much like impure pipes, Angular 2 will see this function call and will try to call it as often as possible in case the value changes. Update a property on your Component or use a pure pipe instead.
    If for some reason you must use a function instead of a property, make it a getter instead of a function since that will cause Angular to think it’s just a normal property.  
  Conclusion

  The combination of Angular 2 and TypeScript is a powerhouse we look forward to using throughout all our projects in the future.
  TypeScript and Angular 2 push you towards designs that are easier to implement and understand than their Angular 1 counterparts. As a developer, Angular 2 and TypesScript has made me happier and more productive.
   Interested in seeing what Angular 2 looks like in action? Check out theCycligent Git Tool which is a version control GUI we built in Angular 2! (don’t worry, it’s free!)
   After you’ve built your new Angular 2 app, you can use the Git Tool to deploy it to the Cycligent Cloud. Or you can sign up for a free 30-day trial here: https://www.cycligent.com
友荐云推荐




上一篇:基础篇章:React Native 之 TextInput 的讲解
下一篇:Sass Mixins For Your Next Project
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

星妍 发表于 2016-10-4 04:37:56
我要让全世界知道这回帖被谁承包了
回复 支持 反对

使用道具 举报

我不識吾誰識 发表于 2016-10-4 05:18:48
梦里见!的等级很高啊!
回复 支持 反对

使用道具 举报

cherry_cms 发表于 2016-10-7 05:13:29
楼主被顶的不轻啊!
回复 支持 反对

使用道具 举报

igcf7438 发表于 2016-10-11 17:11:06
子曾经曰过:男人二十是日立,三十是奔腾,四十是微软,五十是松下,六十是山寨牌…
回复 支持 反对

使用道具 举报

初南 发表于 2016-10-22 10:32:25
我喜欢的女孩要像黛玉一样有才气,像宝钗一样懂事,像可卿一样漂亮,像湘云一样豪爽,像李纨一样忠贞,像探春一样能干,像凤姐一样精明,还要像元春一样有福气,呵呵……
回复 支持 反对

使用道具 举报

tywgv9trsf 发表于 2016-11-4 00:47:38
听说兔子做版主了?
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表