简体   繁体   English

从指令更改控制器$ scope

[英]change controller $scope from a directive

I have a controller: 我有一个控制器:

function myController($scope) {
    $scope.clicked = false;
}

and a directive: 和指令:

function myDirective() {
    return {
        restrict: 'E',

        link: function(scope, elem, attrs) {

            elem.bind('click', function() {
                // need to update controller $scope.clicked value
            });
        },

        template: '<div>click me</div>'; 
        replace: true;
    }
}

and I´m using it like this: 我正在像这样使用它:

<div ng-controller="myController">
    <my-directive></my-directive>
</div>

How can I change the controller value of $scope.clicked ? 如何更改$ scope.clicked的控制器值? thanks! 谢谢!

As you don't use isolated scope in your directive, you can use scope.$parent.clicked to access the parent scope property. 由于您没有在指令中使用隔离范围,因此可以使用scope.$parent.clicked访问父范围属性。

link: function(scope, elem, attrs) {
            elem.bind('click', function() {
                scope.$parent.clicked = ...
            });
        },

I would not recommend using scope.$parent to update or access the parent scope values, you can two way bind the controller variable that needs to be updated into your directive, so your directive becomes: 我不建议使用scope.$parent来更新或访问父作用域值,您可以通过two way bind需要更新的控制器变量two way bind到指令中,因此指令变为:

function myDirective() {
    return {
        restrict: 'E',
        scope: {
         clicked: '='
        },
        link: function(scope, elem, attrs) {

            elem.bind('click', function() {
                // need to update controller $scope.clicked value
                $scope.clicked = !$scope.clicked;
            });
        },

        template: '<div>click me</div>'; 
        replace: true;
    }
}

now pass this clicked from parent: 现在传递来自父级的clicked

<div ng-controller="myController as parentVm">
    <my-directive clicked="parentVm.clicked"></my-directive>
</div>

function myController() {
    var parentVm = this;
    parentVm.clicked = false;
}

I would recommend reading up on using controllerAs syntax for your controller as that would really solidify the concept of using two way binding here. 我建议您阅读对controllerAs使用controllerAs语法的内容,因为这确实可以巩固此处使用双向绑定的概念。

I like to use $scope.$emit for such purposes. 我喜欢将$ scope。$ emit用于此类目的。 It allows to send data from directive to the controller. 它允许将数据从指令发送到控制器。 You should create custom listener in your controller: 您应该在控制器中创建自定义侦听器:

$scope.$on('cliked-from-directive', function(event, data){
        console.log(data)
})

As you can see, now you have full access to your controller scope and you can do whatever you want. 如您所见,现在您拥有对控制器作用域的完全访问权限,并且可以做任何您想做的事情。 And in your directive just to use scope.$emit link: function(scope, elem, attrs) { 并且在您的指令中仅使用scope.$emit链接:function(scope,elem,attrs){

        elem.bind('click', function() {
          scope.$emit('cliked-from-directive', {a:10}) 
        });

Here I've created jsfiddle for you 在这里,我为您创建了jsfiddle

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

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