访问Angular JS中的HTML Parent,Child,Sibling元素

时间:2015-06-26 11:49:25

标签: javascript jquery html angularjs

$scope.delete = function(){
            Here I want to get the t.number
        };
        
<li class="item" ng-repeat="t in items">
                  <div class="row">
                      <div class="col col-10"><i></i></div>
                      <div class="col col-70">
                          <span class="number">{{t.number}}</span>
                          <p class="date">{{t.date}}</p>
                      </div>
                      <div class="col col-20">
                          <i class="ion ion-ios-paper" ng-click="Details()"></i>
                          <i class="ion ion-trash-a" ng-click="delete()"></i>
                      </div>
                  </div>
              </li>

请指导我如何从控制器访问角度js中的html元素。

2 个答案:

答案 0 :(得分:1)

将实体从html传递给控制器​​

试试这个

<强> HTML

<i class="ion ion-trash-a" ng-click="delete(t)"></i>

<强> CTRL

$scope.delete = function(entity){
    console.log(entity.number);
    // if you wanna delete item 
    var index = $scope.items.indexOf(entity);
    $scope.items.splice(index,1);
};

答案 1 :(得分:1)

以下是您需要在ng-click="delete(items, $index)'

中执行的操作
<li class="item" ng-repeat="t in items">
    <div class="row">
        <div class="col col-10"><i></i></div>
        <div class="col col-70">
            <span class="number">{{t.number}}</span>
            <p class="date">{{t.date}}</p>
        </div>
        <div class="col col-20">
            <i class="ion ion-ios-paper" ng-click="Details()"></i>
            <i class="ion ion-trash-a" ng-click="delete(items, $index)"></i>
        </div>
    </div>
</li>

然后,在你的控制器中:

$scope.delete = function(array, index){
    console.log('Item number: ',index); // Get element number
    array.splice(index, 1); // Delete element
};

要解释一下,在您的ng-click中,您发送的数组名称为items,而index(数组中的位置),则为delete } function获取要删除的数组以及数组中项目的位置