2014-09-24 58 views
0

添加指令我有了这个身体Angularjs上点击

<tbody > 
     <tr ng-repeat="con in mycon | array | filter:search | orderBy:predicate:reverse" 
      ng-click="showhistory($event,con.conid)"> 

      <td >{{ con.fname }}</td> 
      <td >{{ con.lname }}</td> 
     </tr> 

</tbody> 

表当行的就是点击我调用这个函数我我的控制器

$scope.showhistory = function ($event,cid) { 
     $event.stopPropagation(); 
     var contentTr = angular.element('<con_history ></con_history>'); 
     contentTr.insertAfter(angular.element($event.target).parent()); 
     $compile(contentTr)($scope); 
     $scope.$apply 
    }; 

,我可以看到的是,con_history标签获取添加。但它没有提供指令。当我添加指令我没有看到BLAH BLAH,我只看到con_history标签

这是我的指令

app.directive('con_history', function() { 
    return { 
     restrict: 'AE', 
     replace: 'true', 
     template: '<tr><td colspan="11">BLAH BLAH</td></tr>' 
    }; 
}); 

感谢您的帮助

回答

1

你不这样做的角度来说,我认为这是你想要做什么:

<tr ng-repeat="con in mycon | array | filter:search | orderBy:predicate:reverse" 
       ng-click="showhistory($event,con.conid)"> 

    <td >{{ con.fname }}</td> 
    <td >{{ con.lname }}</td> 
</tr> 
<con_history ng-if="historyVisible[con.conid]"></con_history> 

在控制器

$scope.historyVisible = {}; 
$scope.showhistory = function ($event,cid) { 
     . 
     . 
     $scope.historyVisible[cid] = true; 
     . 
     . 
}; 
+0

感谢您的回复。我想要做的是当用户点击一行来动态添加一个指令,这反过来指令使ajax调用来填充数据本身。所以我不想要那里的指令,直到它需要。有一个更好的方法吗? – randy 2014-09-24 14:37:34