Call a controller function from a directive without isolated scope in AngularJS

前端 未结 4 2032
花落未央
花落未央 2020-11-29 16:47

I cannot seem to find a way to call a function on the parent scope from within a directive without using isolated scope. I know that if I use isolated scope I can just use \

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 17:19

    Since the directive is only calling a function (and not trying to set a value on a property), you can use $eval instead of $parse (with a non-isolated scope):

    scope.$apply(function() {
        scope.$eval(attrs.confirmAction);
    });
    

    Or better, simply just use $apply, which will $eval()uate its argument against the scope:

    scope.$apply(attrs.confirmAction);
    

    Fiddle

提交回复
热议问题