If you don’t know
AngularJS, it is a
very good Javascript library that allows you to easily build web applications. Here are some random useful tricks that I found for AngularJS users.
TERNARY OPERATOR IN EXPRESSIONS
When you write an expression, like {{ id }}, you can’t use the ternary operator ?:. But you can use the && and || operators. These two lines are identical provided that b always equals true:
a ? b : c
a && b || c
For example:
<p>This feature is {{ feature.isActivated && "activated" || "not activated" }}</p>
This reason why it works is that "a && b" is treated like "if (a) return b; return a;" and "a || b" is treated like "if (!a) return b; return a;".
USING ARRAYS OR OBJECTS INSIDE EXPRESSIONS
You can use arrays or objects directly from inside AngularJS expressions. For example, you can write the list of days of the week like this:
<ul>
<li ng-repeat="day in [1,2,3,4,5,6,7]">Name of the day: {{ ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][day-1] }}</li>
</ul>
This will output:
<ul>
<li>Name of the day: Mon</li>
<li>Name of the day: Tue</li>
<li>Name of the day: Wed</li>
<li>Name of the day: Thu</li>
<li>Name of the day: Fri</li>
<li>Name of the day: Sat</li>
<li>Name of the day: Sun</li>
</ul>
WATCHING TWO VARIABLES ARE ONCE
AngularJS allows you to watch a variable and will call a callback whenever its content is modified. But what if you need to watch two variables at once? You can write this:
function callback() {
...
}
$scope.$watch('a', callback);
$scope.$watch('b', callback);
But you can also write this, which is more elegant:
$scope.$watch('a + b', function() {
...
});
The value of "a + b" will change whenever either a or b changes. Therefore you watch both variable at once.
SYNTAX OF DIRECTIVES
The tutorial of AngularJS writes directives like this: "ng-model". But you can also write: "x-ng-model", "ng:model", "ng_model", "data-ng-model". They are all equivalent and work the same. I like to use the syntax with "data-" because it is standard-compliant.
ADDING A LOADING SCREEN ON AJAX REQUEST
This module will add a loading screen over the whole page whenever an AJAX request is made using AngularJS's $http or $resource. This will prevent the user from clicking anywhere, which is a good solution if you don't want to handle the consequences of clicks during requests. Just don't use this if you load several megabytes of data.
angular
.module('loadingOnAJAX', [])
.config(function($httpProvider) {
var numLoadings = 0;
var loadingScreen = $('<div style="position:fixed;top:0;left:0;right:0;bottom:0;z-index:10000;background-color:gray;background-color:rgba(70,70,70,0.2);"><img style="position:absolute;top:50%;left:50%;" alt="" src="data:image/gif;base64,R0lGODlhQgBCAPMAAP///wAAAExMTHp6etzc3KCgoPj4+BwcHMLCwgAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAQgBCAAAE/xDISau9VBzMu/8VcRTWsVXFYYBsS4knZZYH4d6gYdpyLMErnBAwGFg0pF5lcBBYCMEhR3dAoJqVWWZUMRB4Uk5KEAUAlRMqGOCFhjsGjbFnnWgliLukXX5b8jUUTEkSWBNMc3tffVIEA4xyFAgCdRiTlWxfFl6MH0xkITthfF1fayxxTaeDo5oUbW44qaBpCJ0tBrmvprc5GgKnfqWLb7O9xQQIscUamMJpxC4pBYxezxi6w8ESKU3O1y5eyts/Gqrg4cnKx3jmj+gebevsaQXN8HDJyy3J9OCc+AKycCVQWLZfAwqQK5hPXR17v5oMWMhQEYKLFwmaQTDgl5OKHP8cQjlGQCHIKftOqlzJsqVLPwJiNokZ86UkjDg5emxyIJHNnDhtCh1KtGjFkt9WAgxZoGNMny0RFMC4DyJNASZtips6VZkEp1P9qZQ3VZFROGLPfiiZ1mDKHBApwisZFtWkmNSUIlXITifWtv+kTl0IcUBSlgYEk2tqa9PhZ2/Fyd3UcfIQAwXy+jHQ8R0+zHVHdQZ8A7RmIZwFeN7TWMpS1plJsxmNwnAYqc4Sx8Zhb/WPyqMynwL9eMrpQwlfTOxQco1gx7IvOPLNmEJmSbbrZf3c0VmRNUVeJZe0Gx9H35x9h6+HXjj35dgJfYXK8RTd6B7K1vZO/3qFi2MV0cccemkkhJ8w01lA4ARNHegHUgpCBYBUDgbkHzwRAAAh+QQJCgAAACwAAAAAQgBCAAAE/xDISau9VAjMu/8VIRTWcVjFYYBsSxFmeVYm4d6gYa5U/O64oGQwsAwOpN5skipWiEK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gUOGoJV6KwSmaAYFr54Gs6KHQ6VVnYhMrmxRAraIoaLGpEiRwEx5N5m1J83OTK92v1+Q1ry6vwAIpgLg3dS6yhPbA+nmdqJBHwaZ3OYchtA3BNP2GJf9AD0YCggMlwRTAwqUIygJXwE6BUzBEDCgGsMtoh4+NFOAXpWLHP8y1oh3YZ9FkGlIolzJsqXLlzgkwpgIcwKCAjhzPhSApCcMVTBvCtV4sqbRo0iTshFak1WHfQN6WgmaM5+EiFWqUFxIMJROnDN4UuSX1E5OMVyPGlSKaF+7bqHenogqoKi9fQ/lponIk+zFUAkVthPHc9FLwGA58K17FO9DDBH9PguoMuXjFgSi2u2SWTKvwnpx0MIZ2h/ogLQSlq5QauuW1axJpvac4/QUAW+GKGo2G3ZEwxl4ws5QZE3qzSU9R80NIHO5fUsUMX82/II4drcjFXGR8EdxgPMYoyKHCmhmoM1V9/s9iyIait6x1+mIXEjrNeKmw59SMUSR6l5UE1EjM9txN1049RUUlR771fFfUw1OEJUF38E0TzURJkLbUR31EwEAOwAAAAAAAAAAAA==" /></div>')
.appendTo($('body')).hide();
$httpProvider.responseInterceptors.push(function() {
return function(promise) {
numLoadings++;
loadingScreen.show();
var hide = function(r) { if (!(--numLoadings)) loadingScreen.hide(); return r; };
return promise.then(hide, hide);
};
});
});
GLOBAL ERRORS HANDLER
In web applications you usually handle AJAX errors globally. If the server returns an error, an error message will be displayed, and it's always at the same location in the application no matter the location of the error. This module allows you to define the location where they will be displayed.
angular
.module('globalErrors', [])
.config(function($provide, $httpProvider, $compileProvider) {
var elementsList = $();
var showMessage = function(content, cl, time) {
$('<div/>')
.addClass('message')
.addClass(cl)
.hide()
.fadeIn('fast')
.delay(time)
.fadeOut('fast', function() { $(this).remove(); })
.appendTo(elementsList)
.text(content);
};
$httpProvider.responseInterceptors.push(function($timeout, $q) {
return function(promise) {
return promise.then(function(successResponse) {
if (successResponse.config.method.toUpperCase() != 'GET')
showMessage('Success', 'successMessage', 5000);
return successResponse;
}, function(errorResponse) {
switch (errorResponse.status) {
case 401:
showMessage('Wrong usename or password', 'errorMessage', 20000);
break;
case 403:
showMessage('You don\'t have the right to do this', 'errorMessage', 20000);
break;
case 500:
showMessage('Server internal error: ' + errorResponse.data, 'errorMessage', 20000);
break;
default:
showMessage('Error ' + errorResponse.status + ': ' + errorResponse.data, 'errorMessage', 20000);
}
return $q.reject(errorResponse);
});
};
});
$compileProvider.directive('appMessages', function() {
var directiveDefinitionObject = {
link: function(scope, element, attrs) { elementsList.push($(element)); }
};
return directiveDefinitionObject;
});
});
Usage :
<div class="messagesList" app-messages></div>
Whenever an HTTP request (made using AngularJS's $http or $resource object) returns an error code, or when a non-GET request succeeds, a message will be displayed inside this container for a few seconds. Thanks to this, you almost don't need to worry about errors anymore.
USING HTTP BASIC AUTH
Some web applications use HTTP basic authentication (through HTTPS). You can also use this with AngularJS by setting the value of the "Authorization" header for the $http service.
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode($scope.login.login + ':' + $scope.login.password);
Here is an example code for a login form:
var LoginController = function($scope, $http) {
$scope.login = {};
$scope.login.user = null;
$scope.login.connect = function() {
$http.get('/users/me').success(function(data, status) {
if (status < 200 || status >= 300)
return;
$scope.login.user = data;
});
};
$scope.login.disconnect = function() {
$scope.login.user = null;
};
$scope.$watch('login.login + login.password', function() {
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode($scope.login.login + ':' + $scope.login.password);
});
};
<div ng-controller="LoginController">
<div ng-hide="login.user">
<form action="" ng-submit="login.connect()">
<fieldset>
<legend>Login</legend>
<p><input ng-model="login.login" name="email" type="text" placeholder="Login" required /></p>
<p><input ng-model="login.password" name="password" type="password" placeholder="Password" required /></p>
<p><button type="submit">Login</button></p>
</fieldset>
</form>
</div>
<div ng-show="login.user">
<p>Welcome, {{login.user.userName}}!</p>
<p><button ng-click="login.disconnect()">Logout</button></p>
</div>
</div>
What if the user enters a wrong username or password? The global errors handler (see above) will show an error message.
Note: this is out of topic, but if your server returns a 401 code, the web browser will display a "login dialog", asking the login/password of the user. You don't have any control over this dialog. The solution to prevent this is not to return 401 but another status code (418 I'm a Teapot is a good candidate). A clean way to do so is to add a header to tell the server, like this:
$http.defaults.headers.common['X-StatusOnLoginFail'] = '418';
TWO-WAY BINDING FOR HTML
If you want to write some HTML that you loaded using AJAX, you can use
ng-bind-html-unsafe. But this is a one-directional binding. If the content of the element is modified (thanks to
contenteditable for example), the content of the variable is not updated.
This module allows two-way HTML binding between a variable and an element.
angular
.module('twoWayHTMLBinding', [])
.directive('adminBindHtml', function($timeout) {
return {
link: function compile(scope, tElement, tAttrs) {
var refresh = function() {
scope.$apply(tAttrs.adminBindHtml + ' = "' + tElement.html().replace(/"/g, '\\"') + '"');
$timeout(refresh, 200);
};
scope.$watch(tAttrs.adminBindHtml, function(val, oldVal) {
if (val != oldVal && tElement.html() != val)
tElement.html(scope.$eval(tAttrs.adminBindHtml));
});
$timeout(refresh, 200);
}
};
});
Example:
<div ng-init="htmlData = '<p>Hello!</p>'">
<div admin-bind-html="htmlData" contenteditable="true"></div>
</div>
Note: this may seem obvious, but don't allow non-admins to modify HTML code that can be displayed to anyone, or if you do you must add a
server-side filter.