簡體   English   中英

獲取angularjs上clicked元素的值

[英]Get the value of the clicked element on angularjs

我大約2天前開始使用angular。 我仍然圍繞着如何做很多事情保持着頭腦。 現在,我正在嘗試顯示一個工具提示,其中包含與單擊的“標簽”有關的信息。 我使用ng-toolkit<span> tag <span>元素定義為<span> ,如下所示:

<div id="list-of-words" ng-controller="inlineEditorController" ng-click="hideTooltip()">
    <!-- This is the tooltip. It is shown only when the showtooltip variable is truthful -->
    <div id="tooltip" class="tooltip" ng-click="$event.stopPropagation()" ng-show="showtooltip">
        <input type="text" ng-model="value" />
    </div>
    <div ng-repeat="w in words">
      <span class="tag" ng-click="toggleTooltip($event)">{{w.content}}</span>
    </div>
  </div>

我的控制器是這樣的:

var app = angular.module('myApp', []);

app.controller('inlineEditorController', ['$scope', function($scope){
    $scope.inlineEditor = function(){
        $scope.showtooltip = false;
        $scope.value = 'Edit me.';

        $scope.hideTooltip = function(){
            $scope.showtooltip = false;
        }

        $scope.toggleTooltip = function(e){
            e.stopPropagation();
            $scope.showtooltip = !$scope.showtooltip;
        }
    };
})]);

我試圖做的是更改“編輯我”。 顯示{{w.content}}的內容,但我不知道該怎么做。 到目前為止,我嘗試過的一切都失敗了。

要獲取項目內容,可以使用:

      <span class="tag" ng-click="toggleTooltip($event,w.content)">{{w.content}}</span>

因此,控制器應為:

 $scope.toggleTooltip = function(e,content){
            e.stopPropagation();
            $scope.showtooltip = !$scope.showtooltip;
            $scope.value = content;
    }

您可以使用$event.currentTarget來獲取元素。 e.currentTarget在您的控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM