简体   繁体   English

AngularJS:指令与视图更新之间的通信

[英]AngularJS: Communication between directives and view updation

I am new in AngularJS. 我是AngularJS的新手。 I search throw a lot of information and find some nice solution that should work for me ( AngularJS communication between directives ). 我搜索了很多信息,找到了一些适合我的不错的解决方案( 指令之间的AngularJS通信 )。 Code in first directive looks like that: 第一条指令中的代码如下所示:

angular.module('app')
  .directive('myItem', function () {
    return {
      templateUrl: 'views/item.html',
      restrict: 'C',
      controller: function ($scope, $element) {
        $element.closest('.models-slider').on('afterChange', function(event, slick, currentSlide){
          $scope.$emit('model:updated', currentSlide);
        });
      }
 })

And in second: 第二:

angular.module('app')
  .directive('model', function () {
    return {
      templateUrl: 'views/model.html',
      restrict: 'E',
      controller: function ($scope, $element) {
       $scope.$on('model:updated', function(e, data){
         $scope.item = data;
         console.log('scope',  $scope); // Here I see right value of item
       });
     });
    }
 });

But the problem is inside view/model.html: 但是问题出在view / model.html内部:

<div class="left-block">
  <a class="back" href="#"></a>
  <div class="model-wrapper">
    <div class="front-view active">
      <img src="images/model.png"/>
      <img ng-if="item.frontImage" ng-src="{{ item.frontImage }}"/>
    </div>
    <a class="rotate"></a>
  </div>
</div>

It doesn't update. 它不会更新。 Is there are any mistakes in my code? 我的代码中有任何错误吗? How can I update my view? 如何更新我的看法?

因为afterChange是一个非角度事件,所以在myItem指令中在scope.$apply中发出' model:updated '

Your question is not quite clear though, I will try to give some answer or hints for you consider: 不过,您的问题不太清楚,我将尝试为您提供一些答案或提示:

First, the $scope.$on is not intended for you to pass data between different scopes. 首先, $scope.$on不适用于您在不同范围之间传递数据。 And why do you want to use the restriction of C for your myItem directive? 以及为什么要对myItem指令使用C的限制? how are you using the two directives my-item and model ? 您如何使用my-itemmodel这两个指令? where are they in your views? 您认为它们在哪里?

You can build a bi-directional binding between directive and controller (or controller for another directive). 您可以在指令和控制器(或另一个指令的控制器)之间建立双向绑定 You need to make an isolated scope in the directive myItem : 您需要在指令myItem创建一个隔离的范围:

angular.module('app')
  .directive('myItem', function () {
    return {
      templateUrl: 'views/item.html',
      restrict: 'AEC',
      scope: {
          item: '='   //bi-directional binding
       },
      controller: function ($scope, $element) {
        $element.closest('.models-slider').on('afterChange', function(event, slick, currentSlide){
         // $scope.$emit('model:updated', currentSlide);
         $scope.item = currentSlide;
        });
      }
 })

In the directive template for your model , you can get the item model的指令模板中,您可以获得项目

<my-item item="item"></my-item>

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

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