2015-06-20 154 views
0

HTML作爲內容內的元件內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 
+0

你可以使用jQuery做..

  • hello martin
  • $('#test')。html()and $('#test')。attr('someAttr'); –

    回答

    2

    嘗試這樣

    HTML

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

    控制器

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