AngularJS: how to implement a simple file upload with multipart form?

A real working solution with no other dependencies than angularjs (tested with v.1.0.6) html Angularjs (1.0.6) not support ng-model on “input-file” tags so you have to do it in a “native-way” that pass the all (eventually) selected files from the user. controller The cool part is the undefined content-type and the transformRequest: angular.identity that give at the $http the ability to … Read more

What does ngInject do in the following piece of code?

‘ngInject’; does nothing by itself, it’s just a string literal. A tool called ng-annotate uses it as a flag : if a function starts with ‘ngInject’;, it will be processed by ng-annotate. Basically, ng-annotate will transform to in order to make the code minification-safe. If you’re not using ng-annotate, you can safely ignore or remove the expression. Be … Read more

if else statement in AngularJS templates

Angularjs (versions below 1.1.5) does not provide the if/else functionality . Following are a few options to consider for what you want to achieve: (Jump to the update below (#5) if you are using version 1.1.5 or greater) 1. Ternary operator: As suggested by @Kirk in the comments, the cleanest way of doing this would be to … Read more

ngular is automatically adding ‘ng-invalid’ class on ‘required’ fields

Since the inputs are empty and therefore invalid when instantiated, Angular correctly adds the ng-invalid class. A CSS rule you might try: Which basically states when the field has had something entered into it at some point since the page loaded and wasn’t reset to pristine by $scope.formName.setPristine(true) and something wasn’t yet entered and it’s invalid then the text … Read more

Why do we use $rootScope.$broadcast in AngularJS?

$rootScope basically functions as an event listener and dispatcher. To answer the question of how it is used, it used in conjunction with rootScope.$on; However, it is a bad practice to use $rootScope as your own app’s general event service, since you will quickly end up in a situation where every app depends on $rootScope, and you do not … Read more