通过angularjs中的另一个元素获取元素

时间:2014-12-03 06:32:47

标签: angularjs angularjs-directive angularjs-scope

我要求在单击元素(Button)时应该更改另一个元素属性。 我可以通过jquery来做。但我想忽略我的角度项目中的jquery。

在Html中,

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

在控制器中,

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

在控制器中有没有办法(或不同/最佳方式)?或者需要为此编写指令?

3 个答案:

答案 0 :(得分:5)

你可以保留两个输入来执行此操作

你可以尝试这样的事情

<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>

这是工作plunker

答案 1 :(得分:2)

您无法动态更改输入类型。

您是否考虑使用两个按钮,一个是密码类型,另一个是文本类型。并且您可以使用ng-hide属性在按钮单击时隐藏其中一个(它是一个黑客)。

答案 2 :(得分:2)

你可以轻松动态地改变类型

这是工作代码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";

    };
}]);