gpt4 book ai didi

javascript - 如何使 Controller 触发指令中的函数( Controller 采取指令)

转载 作者:行者123 更新时间:2023-12-03 12:11:49 24 4
gpt4 key购买 nike

在 Angular JS 中,当 HTTP 响应返回成功时,我试图删除按钮,在 Controller 中进行 HTTP 调用,并且它们的 is 指令删除了 ui。那么我怎样才能让 Controller 触发指令中的函数。

angular.module("AccApp").directive('transaction',['Session', function(Session){
return{
restrict:'E',
replace:'true',
scope:{
transaction:'=',
delete:'&',
confirm:'&',
edit:'&'
},
templateUrl:"Partials/transaction.html" ,
controller:'TransactionFileController',



link: function(scope,element,attributes,controller){
//variable to trace the state of content
scope.contentDisplayed=false;
var x ="mmmm";
if(Session.get_user_role() != true || scope.transaction.Confirmed == true){
element.find('#confirm').remove();
}

if(Session.get_user_id() != scope.transaction.UserID && Session.get_user_role() != true){
element.find('#delete').remove();
element.find('#edit').remove();
}

element.find('.transaction-trigger').on("click",function(){
//display and hide the transaction content
element.find(".transaction-details").toggle(400);

scope.contentDisplayed=! scope.contentDisplayed;
//request conent from server
if( scope.contentDisplayed)
{
scope.GetTransThumbs(scope.transaction.TrnID);
}

});


if(scope.transaction.TrnType===true)
{element.find('#type').html("+");}
else if(scope.transaction.TrnType===false)
{element.find('#type').html("-");}
}

}

}]);

<div ng-repeat="trn in group.Transactions" >
<transaction thumbnails="thumbnails" transaction="trn" edit="editTrns(trn)" delete="deleteTrns(trn)" confirm="confirmTrns(trn.TrnID)"> </transaction>
</div>

最佳答案

说实话,我还没有浏览过你所有的代码。无论如何,从 Controller 执行指令内函数的最常见方法是这样实现的:

(由于您已经在使用隔离范围,因此事情变得更容易)

未经测试的代码

指令

    angular.module('myapp').directive('myDirective',function(){

return {
restrict:'E,A',
replace: true,
templateUrl: 'your template.html path',
scope:{
myFunction:'=' // bidirectional binding here...
},
link : function(scope,ele,attrs){
scope.internalCtrl = scope.myFunction || {};
scope.internalCtrl.directiveFunction = function(){ do stuff }
}
}
});

Controller

    angular.module('myController',function($scope){

$scope.controllerFunction = {};
});

HTML

     <button ng-click="controllerFunction.directiveFunction()">Click me to call the directive function!</button>
<div my-directive my-function="controllerFunction"></div>

关于javascript - 如何使 Controller 触发指令中的函数( Controller 采取指令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com