简体   繁体   中英

change attribute of target when click using angularJS

<ul class="todo-list">
    <li ng-repeat="todo in todos">{{todo.todoContent}}<span class="pull-right glyphicon glyphicon-check" ng-class="{checked:todo.done}" ng-click="todoCheck(todo)" title="check this todo">check</span></li>
  </ul>

Now I want change title attribute to "uncheck this todo" and text "check" to "checked" when I click the span element to check target todo.
Anyone can help here. Thanks very much.

example fiddle

If you want more fine grained control over the element you clicked, you can do this by using the $event in the ng-click handler.

like

ng-click="todoCheck($event, todo)"

I have used jquery to set the title and content and updated your fiddle.

Here is the updated fiddle

$scope.todoCheck = function ($event, todo) {

  var spanElement = $event.srcElement; $(spanElement).attr('title', "uncheck this todo"); $(spanElement).html("checked"); todo.done = !todo.done; 

};

<li ng-repeat="todo in todos">
    {{todo.todoContent}}
    <span class="pull-right glyphicon glyphicon-check" 
    ng-class="{checked:todo.done}" ng-click="todoCheck(todo)" 
    title="{{!todo.done && 'check this todo' || 'uncheck this todo'}}">
        {{!todo.done && 'check' || 'checked'}}
    </span>
</li>

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