2

I have a requirement that when clicking an element(Button) should change another element property. I can do by jquery. But i want to ignore the jquery in my angular project.

In Html,

<input type=password />
<button ngclick="changeToTxt()">Change type password to text</button>

In controller,

app.controller('loginPage', function ($scope) {
   $scope.changeToTxt = function () {
     **// need to get the password element and need to change type = text.....**
   }
});

Is there a way(or different/best way) by doing in controller? or need to write a directive for this?

0

3 Answers 3

5

you can keep two inputs to do this

you can try something like this

<input type="password" ng-model="pwd" ng-show="!showText" />
<input type="text" ng-model="pwd" ng-show="showText" />

<button ng-click="showText = !showText ">Change type password to text</button>

here is the working plunker

Sign up to request clarification or add additional context in comments.

1 Comment

Nice and best way for this scenario.
2

u can easily change the type dynamically

here is the working code fiddle

<div ng-app="app">
  <div ng-controller="MainController">
    <input type={{test}} />
    <input type="button" value="change" ng-click="changeType()"/>
  </div>
</div>


angular.module('app', []).
  controller('MainController', ['$scope', function ($scope) {
    $scope.test = "text";

     $scope.changeType = function () {
       $scope.test = "password";

    };
}]);

4 Comments

Is this cross - browser compatible ? I think it does not work on IE. Have you tried it on different browsers?
yes. i have tested in all platform. it will work in all.
Cool, i want to do like this only. Thanks.
2

You cannot change the input type dynamically.

Have you considered using two buttons , one of type password and other of type text. And you can use the ng-hide property to hide one of them on button click( it's a hack).

1 Comment

Good one. But i thought of passing the element and get that element inside controller scope.(as jquery or javascript direct approach).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.