Top 5 Common Angular JS Mistakes Web Developers are Making
AngularJS is one of the most invincible and profound Javascript frameworks launched by Google in 2009. The main function of this framework is to illuminate the web development life cycle that makes it immaculate for prototyping smaller applications.
Thanks to its flexible and secure structure, it quickly became a popular choice among many web developers. It has been used to develop Single Page Applications that can render amazing User Experience and User Interface. In simpler words, it builds scalable applications that can be used on a variety of devices.
Though AngularJS is an extraordinary JavaScript framework that provides multiple features, it certainly has created many challenges and pitfalls for the developers while creating dynamic web applications. In order to leverage AngularJS, there are few mistakes you need to avoid that you are probably making.
Here is the list of top 5 most common AngularJS mistakes:
1. MVC Directory Structure
AngularJS is an MVC framework that offers MVC components to build web applications. The components are not evidently defined as a framework such as Backbone.js, but the architectural pattern still perfectly fits.
The best way to work with MVC framework is to group files together depending on their file type.
As an instance:
templates/
_login.html
_feed.html
app/
app.js
controllers/
LoginController.js
FeedController.js
directives/
FeedEntryDirective.js
services/
LoginService.js
FeedService.js
filters/
CapatalizeFilter.js
Seems familiar? Well, if you are from Rails background, this layout would seem obvious. However, once you start to scale your application, this layout would cause various folders to be open at the same time. Be it Vim, Visual Studio, or Sublime, investing a lot of time scrolling through the directory tree becomes a common practice.
Therefore, it is always suggested to group files based on their features instead of type.
Like:
app/
app.js
Feed/
_feed.html
FeedController.js
FeedEntryDirective.js
FeedService.js
Login/
_login.html
LoginController.js
LoginService.js
Shared/
CapatalizeFilter.js
This directory structure makes it extremely easy to find files related to a specific feature, which will automatically accelerate your development.
2. Dependency Injection
Dependency injection is apparently one of the best patterns of AngularJS. It makes testing a lot easier for developers while making it more evident upon which a specific object relies. The framework is extremely scalable on how things can be injected.
The easiest version of Dependency Injection needs simply passing the dependency name into the function like:
var app = angular.module(‘app’,[]);
app.controller(‘MainCtrl’, function($scope, $timeout){
$timeout(function(){
console.log($scope);
}, 1000);
});
It is clearly evident that $scope and $timeout are the two arguments on which MainCtrl depends upon.
This will work well unless you decide on going to production and wish to minify the code.
Here is the example using UglifyJS:
var app=angular.module(“app”,[]);app.controller(“MainCtrl”,function(e,t){t(function(){console.log(e)},1e3)})
if you look closely you would realize there isn’t any way to notify AngularJS about MainCtrl’s dependency.
Therefore, it is suggested to pass an array of strings to dependencies, where the last array element being a function which considers all the dependencies as arguments.
app.controller(‘MainCtrl’, [‘$scope’, ‘$timeout’, function($scope, $timeout){ $timeout(function(){ console.log($scope); }, 1000); }]);
And then minify the code with evident dependencies that the framework knows how to interpret.
app.controller(“MainCtrl”,[“$scope”,”$timeout”,function(e,t){t(function(){console.log(e)},1e3)}])
3. Using jQuery
Almost every developer is familiar with jQuery, a popular library for AJAX operations, for managing events and creating DOM manipulation easy. On the other hand, AngularJS is a framework that is employed in the development of flexible applications. It is all about developing and testing different application, and thus cannot be employed in augmentation of HTML pages. Therefore, it cannot be used with jQuery.
What you can do is to take your code beyond HTML syntax simply it has been declared.
The Javascript Framework has enough features and thus, a developer must figure out what features they can use before opting jQuery. If you need DOM manipulation, do it only in directives, which keeps jQuery out of your application development.
4. Not optimizing the application
If you don’t optimize your application, it will surely lead to AngularJS errors.
Have a look at a code inside the controller that will not only work slow but migh also give you an error.
this.maxPrice = ‘100’;
this.price = ’55’;
$scope.$watch(‘MC.price’, function (newVal) {
if (newVal || newVal === 0) {
for (var i = 0; i < 987; i++) {
console.log(‘ALL YOUR BASE ARE BELONG TO US’);
}
}
});
The best solution for this would be to put a timeout in place on the input.
5. Overlooking controllerAs Syntax
To assign a model to a controller object, you need to use $scope. However, it doesn’t considered to be the best way to perform the task. Using controllerAs Syntax is considered to be the most efficient and the best alternative.
As an instance:
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()
}
As a matter of fact, most of these mistakes occur when you least expect them. It indeed helps when you know what causes the error and how to fix it without having to invest a lot of efforts.
If you want to make the most of this framework, then it is mandatory that you prevent these common mistakes from happening in your code to get the desired goals in a short span of time.
Author Bio:
Jason is expert in developing WordPress Websites and earns his daily bread from the same. He likes to stay updated with the latest tech advancements and also loves to contribute to the same. For those who are looking to Hire WordPress Programmer can match pace with the competitiveness can count on Jason for it.
Comments
NO COMMENT YET
Leave a response