简体   繁体   English

$ scope。$ apply()不调用函数

[英]$scope.$apply() not calling function

I have a need for a custom click directive, which executes the passed code using scope.$apply(). 我需要一个自定义的click指令,该指令使用scope。$ apply()执行传递的代码。

$(elem).on('click', function(){
    scope.$apply(attrs.wdClick);
});

This works fine if I pass something like wd-click="something = !something". 如果我通过类似wd-click =“ something =!something”的命令,则可以正常工作。 But when I try to call a $rootScope function it does not work, however it does work when using the default ng-click. 但是,当我尝试调用$ rootScope函数时,它不起作用,但是当使用默认的ng-click时,它却起作用。

wd-click="$root.someFunction()" //this does not call the function but ng-click does

I have tried updating the directive to make it work 我已经尝试过更新指令以使其起作用

$(elem).on('click', function(){
    $rootScope.$apply(attrs.wdClick);
});

But this does not work either. 但这也不起作用。 Any ideas? 有任何想法吗?

attrs.wdClick is a string, so passing it to $apply won't do anything. attrs.wdClick是一个字符串,因此将其传递给$apply不会执行任何操作。 To call the function you can pass the string to $eval 要调用该函数,您可以将字符串传递给$eval

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

You should wrap your code in function(){} 您应该将代码包装在function(){}中

scope.$apply(function(){
    attrs.wdClick() // this is some sunction I suppose
});

Would you want to call your rootscope method in an another controller? 您想在另一个控制器中调用rootscope方法吗? If I understand correctly, try to use this way : 如果我理解正确,请尝试使用这种方式:

 angular.module('app', []) .controller('Ctrl', function Ctrl1($scope, $rootScope) { $rootScope.blah = 'Hello'; $scope.yah = 'World' }) .directive('myTemplate', function() { return { restrict: 'E', templateUrl: 'my-template.html', scope: {}, controller: ["$scope", "$rootScope", function($scope, $rootScope) { console.log($rootScope.blah); console.log($scope.yah); $scope.test = function(arg) { console.log(arg); } }] }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <div ng-app="app"> <div ng-controller="Ctrl"> <my-template></my-template> </div> <!-- my-template.html --> <script type="text/ng-template" id="my-template.html"> <label ng-click="test($root.blah)">Click</label> </script> </div> 

Also you can try on jsfiddle, http://jsfiddle.net/mg74b/24/ 您也可以尝试jsfiddle, http://jsfiddle.net/mg74b/24/

Is there a reason you are trying to use attrs instead of a scope property? 您是否有理由尝试使用attrs而不是scope属性? Also, you should use $timeout instead of $apply. 另外,您应该使用$ timeout而不是$ apply。

angular
        .module('app', [])
        .directive('myDirective', myDirective);

myDirective.$inject = ['$timeout'];
function myDirective($timeout) {
    return {
        restrict: 'E',
        templateUrl: 'my-template.html',
        scope: {
            wdClick: '='
        },
        link: linkFn
    };

    function linkFn(scope, element) {
        element.on('click', function () {
            $timeout(scope.wdClick);
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM