Upload An Image Using Angularjs

- 1 answer

enter image description here

            <label class="item item-input item-stacked-label" ng-class="{ 'has-error-lr' : userForm.firstName.$invalid, 'valid-lr' : userForm.firstName.$valid}">
                <span class="input-label">First name</span>
                <input type="text" name="firstName" ng-model="user.firstName" placeholder="Type first name" ng-pattern="/^[a-zA-Z\.'\s]*$/" required>

                <div class="form-error wrap-text" ng-show="userForm.firstName.$error && userForm.firstName.$invalid && userForm.$submitted">

                    Please enter valid a name, special characters or numbers are not allowed.

            <label class="item item-input item-stacked-label" ng-class="{ 'has-error-lr' : userForm.lastName.$invalid, 'valid-lr' : userForm.lastName.$valid}">
                <span class="input-label">Last name</span>
                <input type="text" name="lastName" ng-model="user.lastName" placeholder="Type last name" ng-pattern="/^[a-zA-Z\.'\s]*$/">
                <div class="form-error wrap-text" ng-show="userForm.lastName.$error && userForm.lastName.$invalid && userForm.$submitted">
                    Please enter valid a name, special characters or numbers are not allowed.

            <label class="item item-input item-stacked-label" ng-class="{ 'has-error-lr' : userForm.userEmail.$invalid, 'valid-lr' : userForm.userEmail.$valid}" ng-if="!isEditing">
                <span class="input-label">Email</span>
                <input type="email" name ="userEmail" ng-disabled="isEditing" ng-model="user.userEmail" placeholder="Type email">
                <div class="form-errors"  ng-messages="userForm.userEmail.$error" ng-if="userForm.userEmail.$invalid && userForm.$submitted" ng-messages-include="templates/form-errors.html"> </div>

            <label class="item item-input item-stacked-label" ng-class="{ 'has-error-lr' :$invalid , 'valid-lr' :$valid}">
                <span class="input-label">Contact no</span>
                <input type="tel" name="contact" ng-model="" placeholder="Type contact number" pattern="^\d{10}$" maxlength="10" required>
                <div class="form-errors"  ng-messages="$error" ng-if="$invalid && userForm.$submitted" ng-messages-include="templates/form-errors.html"> </div>

            <label class="item item-select item-input" ng-if="!isEditingSelfAccount" ng-class="{ 'has-error-lr' : userForm.role.$invalid , 'valid-lr' : userForm.role.$valid}">
                <span class="input-label">Select role</span>
                <div data-tap-disabled="true">
                    <select ng-model="user.role" ng-change="setClassesArrayToUserObject()" name="role" required>
                        <option value="admin" ng-if="userRoleType.isAdmin">Admin</option>
                        <option value="teacher" ng-if="userRoleType.isAdmin">Teacher</option>
                        <option value="student" ng-if="userRoleType.isAdmin || userRoleType.isTeacher">Student</option>
                <div class="form-errors"  ng-messages="userForm.role.$error" ng-if="userForm.role.$invalid && userForm.$submitted" ng-messages-include="templates/form-errors.html"> </div>

the page is made using angularjs and ionic. I am new to angular and ionic. Need to upload an image before first name or beside it. This ionic icon can be used "" to upload an image. Used ng-upload, but was not able to understand it properly.

How to do that?



Did you install Angular File Upload? Here are the steps to use Angular File Upload: We need to install Angular File Upload before using it in our code.

  1. Open Command Prompt
  2. Go to your project root directory
  3. run command npm install angular-file-upload After installing angular-file-upload we need to add it in our angular module as dependency.
  4. To inject dependency run below command:

    var app = angular.module('my-app', [ 'angularFileUpload' ]);

Now you can use it anywhere in code.

For example: In Html:

<input type="file" nv-file-select uploader="uploader"/><br/>

In anuglar Module:

.module('app', ['angularFileUpload'])
.controller('AppController', function($scope, FileUploader) {
    $scope.uploader = new FileUploader();

You can find simple example on this link. I hope it helps.