在AngularJS中有条件地调用函数

时间:2014-09-03 10:14:09

标签: jquery angularjs angularjs-ng-click

我正在使用与AngularJS一起提供的轻量级jQuery向删除按钮添加一个属性(这就是我找到解决方案的源代码所示),如下所示:

if (item.CustomerGuid == item.OwnerCustomerGuid) {
    $scope.isDeleteDisabled = false;
    $('#deleteFreight').attr("ng-click", "FreightGroup_delete(" + item.ListGuid + ", " + item.CustomerGuid + ")");
} else {
    $scope.isDeleteDisabled = true;
}

当我检查时,变化会被反映出来,但是当我尝试点击按钮时,不会调用所需的功能。

需要帮助。

.cshtml(前):

<button class="btn btn-danger" ng-disabled="isDeleteDisabled" 
id="deleteFreight">Delete Freight Group </button>

(后):

<button class="btn btn-danger active" ng-disabled="isDeleteDisabled" 
ng-click="FreightGroup_delete(7338bb3a-9e6b-49d7-be08-7b60ff47ba61, 
  ce3adfcc-d8a1-4a6e-a2b7-d15dc1980384)" id="deleteFreight">
Delete Freight Group </button>

2 个答案:

答案 0 :(得分:1)

使用jquery函数不会影响范围。 请查看以下代码

if (item.CustomerGuid == item.OwnerCustomerGuid) {
    $scope.isDeleteDisabled = false;
    $scope.dynamicAttr= [
       { attr: 'ng-click', value: "FreightGroup_delete(" + item.ListGuid + ", " + item.CustomerGuid + ")" }
    ];
} else {
    $scope.isDeleteDisabled = true;
}

并且您的HTML看起来像这样

<button class="btn btn-danger" ng-disabled="isDeleteDisabled" 
id="deleteFreight" dyn-attrs="dynamicAttr">Delete Freight Group </button>

让我知道它是否有效。

答案 1 :(得分:1)

不要将jquery与angularjs混合使用。如果我正确理解你想要实现的目标,这应该是正确的方法。

JS:

$scope.item = item;

HTML:

<button 
  class="btn btn-danger" 
  ng-click="FreightGroup_delete(item.ListGuid, item.CustomerGuid)" 
  ng-disabled="item.CustomerGuid !== item.OwnerCustomerGuid" 
  id="deleteFreight">Delete Freight Group 
</button>