In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are common AngularJS errors", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are common AngularJS errors" this article.
1. In AngularJS, regardless of the actual situation, it is declared blindly by using anonymous functions.
Always assign your objects and functions to specific variables. In this way, you will find that control and change work will become easier, and the code itself will be cleaner and easier to consult. In addition, you can easily split the code in different files, which is very important for the subsequent maintenance of the application.
At the same time, this approach is also important to ensure testability; when the declaration relationship is clear, the testing of the code becomes very simple. As developers, you should make your code more intuitive and easy to understand, all of which will pay off over a long work cycle. All in all, following this principle will help you significantly reduce the number of errors and bring a lot of benefits.
two。 $applyAsync is not used
Since there is no polling mechanism for calling $digest () in AngularJS, it only executes existing instructions. $applyAsync can help us effectively delay expression parsing until the next $digest () cycle. You can choose to use $applyAsync manually or automatically.
3. Use jQuery
When handling events and implementing AJAX operations, jQuery has become a set of library options that can significantly reduce the difficulty of DOM operations. On the other hand, AngularJS is designed to provide a framework for creating extensible applications. Its main focus is on the development and testing of the application, so it cannot be used to implement extensions in HTML pages. In other words, we don't need to use jQuery in this case. At this point, the ideal choice is to make your code go beyond the HTML syntax after making the declaration.
AngularJS has a number of features to choose from, and developers should be able to find the best implementation instead of using jQuery. If you have to do the DOM operation, you can do it directly in the instruction-- it doesn't have to involve jQuery.
4. The application is not optimized
Lack of necessary optimizations will almost certainly lead to AngularJS errors. The following example explains how the code in the controller slows down processing and causes potential errors:
This.maxPrice = '100 million; this.price =' 55 years; $scope.$watch ('MC.price', function (newVal) {if (newVal | | newVal = 0) {for (var I = 0; I < 987; iTunes +) {console.log (' ALL YOUR BASE ARE BELONG TO US');})
As a solution, we can try to add a timeout to the input.
5. Use scope isolation when not necessary
If you want to use an instruction and make sure that it is only applied to a single location and does not cause other unexpected conflicts in the environment, it is not necessary to use the scope isolation mechanism-this may lead to errors. It is important to emphasize here that we cannot use two scope isolation instructions in a single element at the same time. In addition, scope isolation can cause problems when doing operations such as nesting, event handling, or inheritance.
6. Use too many watchers
For each link, AngularJS creates an observer for it. At each digest stage, the viewer is compared and evaluated with previous bindings, a process AngularJS calls dirty checking. You can imagine how many observation programs will eventually be left when this series of processes are over.
In fact, the method of constraining the number of observers is very simple, you only need to make sure that the scope model will not change without observing it. This means that the number of observers will be significantly reduced, and the chance of causing errors will be significantly reduced.
7. Ignore controllerAs syntax
Scope is often used to assign a model to a controller object. In this case, however, injecting scope is generally not the best way to handle it. Instead, we recommend that you choose controllerAs syntax to achieve this goal. Let's learn how to use controllerAS syntax to define the model through the code:
Function MainController ($scope) {this.foo = 1; var that = this; var setBar = function () {/ / that.bar = {someProperty: 2}; this.bar = {someProperty: 2};}; setBar.call (this); / / there are other conventions: / / var MC = this; / / setBar.call (this); when using 'this' inside setBar ()}
ControllerAs syntax can significantly improve the confusion of results, especially when we need to deal with a large number of nested scope. It can also be used in a number of other ways to help us build applications more easily.
8. The load strength is too high
Many Web developers tend to use a large number of high-intensity processing threads in AngularJS, but after overcoming this bad habit, we can use worker threads and avoid many errors caused by high-intensity processing tasks. High-intensity processing tasks can cause the browser to get stuck. Worker thread is a good way to solve this kind of problem, we only need to use worker thread mechanism directly, which can significantly reduce the difficulty of dealing with large-scale complex objects.
9. Failed to use controllerAS syntax as needed
ControllerAs syntax is extremely practical and can help people to have better code results when building applications. One of the common mistakes of developers today is that they fail to make full use of them and realize their great potential. In fact, when we assign a set of models to a controller tool, the controllerAs syntax should be the implementation mechanism of *. It also has a series of other highly practical features. Let's learn about it through a code example:
Function MainController ($scope) {this.title = 'Some title'; $scope.$watch (angular.bind (this, function () {return this.title;}), function (newVal, oldVal) {/ / handle changes});}
10. Failed to fully understand the parser
Basically, the involvement of the parser increases the actual time we take to load the view. You should not overuse the parser, as this means that the loading time of the site will be extended, which will eventually lead to an unbearable access experience.
Some of these errors can lead to results that Web developers don't want to see. As long as you take these problems into account and solve them in your development work, many errors will not occur at all, which eliminates the possibility that we will be forced to rewrite code from scratch.
The above is all the contents of the article "what are the common AngularJS errors?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.