简体   繁体   English

Angular.js - 控制器通信指令

[英]Angular.js — Directive to controller communication

I am very new to angular so please excuse my lack of understanding. 我很有棱角,所以请原谅我缺乏理解。

I have a directive called "draggable" which I want to be able to track the x position of and perform some logic on it in the controller. 我有一个名为“draggable”的指令,我希望能够在控制器中跟踪它的x位置并对其执行一些逻辑。 When the user drags the element (a stick figure) to the right, additional stick figures should appear directly behind it. 当用户将元素(一个简笔画)向右拖动时,额外的棒图应该直接出现在它后面。 The controller should know the x position and based upon where it is, increment a counter which will dictate how many stick figures appear behind the draggable element. 控制器应该知道x位置并根据它的位置,增加一个计数器,该计数器将指示可拖动元件后面出现多少个棒图。

This code does not currently work as the controller does not have receive the value of x. 此代码当前不起作用,因为控制器没有接收x的值。

My directive: 我的指示:

app.directive('draggable', function() {
    return {
        restrict: 'A',
        scope: "=x",
        link: function (scope, element, attrs) {
            $(element).draggable({
                containment: "parent",
                axis: "x",
                drag: function(){
                    scope.x = $(this).offset().left;
                }
            });
        }
    };
});

My controller: 我的控制器:

app.controller("main-controller", function($scope) {
    $scope.range = function(n) {
        return new Array(figures);  
    };
    $scope.$watch("x", function(){
        console.log($scope.x);
        figures = x / (stick_figure_height)
    });
});

My HTML: 我的HTML:

<div class="human-slider" ng-controller="main-controller">
    <div draggable class="human-draggable">
        <img src="images/stickfigure.png"/>
    </div>
    <div ng-repeat="i in range()">
        <img src="images/stickfigure.png"/>
    </div>
</div>

The reason the controller was not picking up the updated value of x from the draggable directive was because of where the value of x is being updated. 控制器没有从draggable指令中获取x的更新值的原因是因为x的值正在更新的位置。 X is updated in a turn that has been created in a method outside of the angularJS library (the drag event handler). X在一个转弯中更新,该转弯是在angularJS库(拖动事件处理程序)之外的方法中创建的。 The solution to this problem was to use $.apply which will update the binding. 这个问题的解决方案是使用$ .apply来更新绑定。

The updated code: 更新的代码:

// Create our angular app
var app = angular.module('celgeneApp',[]);


// Main controller 
app.controller("main-controller", function($scope) {
    $scope.x = 0;
    $scope.figures = 0;
    $scope.range = function(n) {
        return new Array($scope.figures);  
    };
    $scope.$watch('x', function(){console.log($scope.x);});
});

// Draggable directive
app.directive('draggable', function() {
    return {
        restrict: 'A',
        scope: false,
        link: function (scope, element, attrs) {
            $(element).draggable({
                containment: "parent",
                axis: "x",
                drag: function(){
                    // Need to use $apply since scope.x is updated 
                    // in a turn outside a method in the AngularJS library.
                    scope.$apply(function(){scope.x = element.offset().left;});
                }
            });
        }
    };
});

You can communicate between a directive and a controller through a service. 您可以通过服务在指令和控制器之间进行通信。 A directive can also access a controller's scope variables via parameters. 指令还可以通过参数访问控制器的范围变量。 You can access the variables in different ways, depending on your needs: 您可以根据需要以不同方式访问变量:

  • As just text with the @ prefix 只是带有@前缀的文本
  • With a one way binding with the & prefix 使用单向绑定&前缀
  • With a two bay binding with the = prefix 使用带有=前缀的两个托架绑定

Check out this excellent article about directives, especially the scope section 查看这篇关于指令的优秀文章 ,尤其是范围部分

Take a look at this directive I made, it is just a wrapper around jQuery's draggable just like yours, maybe you can get some ideas: 看看我制作的这个指令,它只是像你一样的jQuery可拖动的包装器,也许你可以得到一些想法:

angular-draggable 角可拖动

Check my this for how parent controller and directive communicates :) 检查我的这个父控制器和指令如何通信:)

http://plnkr.co/edit/GZqBDEojX6N87kXiYUIF?p=preview plnkr http://plnkr.co/edit/GZqBDEojX6N87kXiYUIF?p=preview plnkr

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

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