How to catch Angular ui-router resolve errors

It’s more than annoying to have just a blank page and no errors in console when your issue is inside ui-router resolve. Finally I found a pretty nice solution, don’t know why it’s not provided “from the box”. In case of error ui-router sends specific event $stateChangeError that we could listen for:

[javascript]
$rootScope.$on(‘$stateChangeError’, function(event, toState, toParams, fromState, fromParams, error){
console.error(error);
});
[/javascript]

and now wow! we have an exact error in console and no need to guess what’s wrong with your code anymore.

AngularJS: ng-click и ng-disabled

Учтите что ng-disable работает только на элементах формы, поэтому тут

[html]

<div ng-click="doAction()" ng-disabled="buttonDisable">Click Me</div>

[/html]

директива ng-click отрабатывает клик.

Если необходимо использовать ng-disable могу предложить такой вот хак:

[html]
<div ng-click="buttonDisable || doAction()" ng-disabled="buttonDisable</div>
[/html]

 

P.S.: Большоя спасибо Vitalii Hudyma за планкер с кодом.

AngularJS и проверка данных формы

AngularJS Validation

Пост состоит из следующих частей:

  • Проверка данных в HTML5
  • AngularJS расширения для валидации
  • Свои кастомные проверки данных на AngularJS
  • Вывод сообщений об ошибках и ng-messages

 

Continue reading

Angular: $scope.$on() – listen to several events

I found it quite strange that AngularJS does not have possibility to watch several Angular-events, i.e.:

[javascript]
$scope.$on([‘user.login’, ‘user.logout’], callback);
[/javascript]

and I decided to extend $on method, to make it handle such case:

[javascript]
var $onOrigin = $rootScope.$on;
$rootScope.$on = function(names, listener) {
var self = this;

if (!angular.isArray(names)) {
names = [names];
}

names.forEach(function(name) {
$onOrigin.call(self, name, listener);
});

};
[/javascript]

Sandbox for this code you can find here.