简体   繁体   English

单击 angularJs 时更改图标

[英]To change the icon upon click with angularJs

Here is the Html displaying only one icon now, but what I want is that whenever that edit icon is being clicked it changes to save icon immediately.这是现在仅显示一个图标的 Html,但我想要的是,每当单击该编辑图标时,它都会立即更改为保存图标。

Below is the html:下面是html:

<td style="padding:5px!important;">
                            <i class="fa fa-edit" ng-click="updateEmp()">
                      </td>

And down below is the JS file, is there any way to add the html bind to the JS File?下面是JS文件,有没有办法将html绑定添加到JS文件? JS Code: JS代码:

$scope.updateEmp =  function (){
            $scope.isReadonly = false;
            console.log("update Employee");
        }

You can use ng-class and toggle the class on click as follows.您可以使用ng-class并在单击时切换类,如下所示。

ng-class="{'fa fa-save' : toggle, 'fa fa-edit' : !toggle}"

DEMO演示

 var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl',function($scope){ $scope.updateEmp = function (){ $scope.toggle = !$scope.toggle; } });
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <h1 style="padding:5px!important;"> <i ng-class="{'fa fa-save' : toggle, 'fa fa-edit' : !toggle}" ng-click="updateEmp()"></i> </h1> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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