简体   繁体   中英

angularjs update value of ng-model to set default value

I am trying to set default value of username ( $scope.userName) and password ( $scope.password),I have tried with ng-init but its not working, but its not working here is my code :

APP.directive('loginUser', ['$http','$interval','$rootScope','$timeout', function ($http,$interval,$rootScope,$timeout) {
function link($scope, element, attrs) {

    $scope.userName = 'Ali';
    $scope.password = 'pass';

}

return {
    link: link
};


}]);

here is my html

<div class="modal-body" login-user  >

    <form class="custom-form" name="loginForm" ng-submit="loginSubmit()" novalidate>

        <label class="museo_sans500" for="email">Nome utente o indirizzo email</label>
        <input type="text" class="form-control museo_sans300" id="userName" placeholder="Nome utente o indirizzo email" ng-init="userName='<?php echo "Yesss" ?>'"   name="UserName" minlength="5" ng-model="userName" required="required">

      <div class="form-group">
        <label class="museo_sans500" for="password">Password</label>
        <input type="password" class="form-control museo_sans300" id="password" placeholder="Password" name="password"  minlength="5" ng-model="password"  required="required">
      </div>
      <div class="form-group">
        <label class="museo_sans700"><a href="javascript:void(0)"  ng-click="forgotPasswordShow()">Hai dimenticato la password?</a></label>
        <label class="museo_sans700" style="margin-left: 30px;"><input type="checkbox" ng-model="rememberMe" name="remember-me"> Ricordami</label>
      </div>
      <button type="submit" class="btn play-now museo_sans500 m-t-20px login-btn"  ng-disabled="loginForm.$invalid">ENTRA</button>
      <div class="or museo_sans500">oppure</div>

    </form>
  </div>

Thanks in advance

instead of ng-init you should assign default value in your controller/directive and in input field just use ng-model .

In directive :

$scope.userName = 'Ali'; // default name

in html:

<input type="text" class="form-control" id="userName" placeholder="name" name="UserName" ng-model="userName" required="required">

PLUNKER DEMO

For the value of the attribute placeholder use:

<input type="password" class="form-control museo_sans300" id="password" placeholder="{{password}}" name="password"  minlength="5" ng-model="password"  required="required">

Pretty sure this should work. Let me know how if you continue to have problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM