簡體   English   中英

訪問控制器函數內部的元素內部html和屬性值

[英]Access the element inner html and attribute values inside controller function

HTML內容為,

<div ng-controller="logCtrl">
  <ul>
    <li ng-click="logMe" someAttr="hello ricky">hello martin</li>
  </ul>
</div>

JavaScript內容為,

var app = angular.module('app',  []);
app.controller('logCtrl',  function ($scope, $element) {
  $scope.logMe = function(){
    console.log("html content is " + /*---- how can i print $element's inner html text here --- */)
    console.log("attribute value is " + /*---- how can i read the li element's someAttr value here --- */)
  }
})

這必須在控制台中打印此消息(當用戶單擊鼠標時),

html content is hello martin
attribute value is hello ricky

這樣嘗試

的HTML

 <li ng-click="logMe($event)" someAttr="hello ricky">hello martin</li>

控制者

$scope.logMe = function(e){
    var value=e.target.attributes.someAttr.value;
}

暫無
暫無

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

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