javascript - 访问 Controller 函数内的元素内部 html 和属性值

标签 javascript angularjs

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>

Controller

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

关于javascript - 访问 Controller 函数内的元素内部 html 和属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951330/

相关文章:

javascript - 在 SailsJS 中将 Angular 表单数据发布到 MongoDB

javascript - 如何跨多个选项卡维护登录 session ?

javascript - 将点击事件添加到元素,但不添加 Angular 子元素

javascript - 使用两个数据绑定(bind)访问 Angular Directive(指令)中的范围

javascript - UI-Grid 在单元格下拉列表中显示未定义

javascript - 调用基于 jQuery Cookie 的函数

javascript - 如何在使用 CoffeeScript 的 Framer 中 setTimeout() ?

javascript - 使用 Maven 运行 AngularJS 测试

javascript - 如何在 JavaScript 中将一个表情符号字符转换为 Unicode 代码点编号?

javascript - 在 IE 中重复警告 "Message from web page: null"