javascript - How to make a variable change in one directive to get reflected in another directve - Stack Overflow

How do i change the value of bar from directive2 so that it is reflected in directive1If i make the sco

How do i change the value of bar from directive2 so that it is reflected in directive1

If i make the scope:false it is happening.
Is there any other way, to make this happen.(because in the code i am writting , i cannot make scope:false).
My basic requirement is to make one directive to talk to another.
Here you can try the plunkr version of the below code

HTML snippet

<body ng-controller="MainCtrl">
  this is directive1: <div directive1></div>.<br/>
  this is directive2: <div directive2></div>.
</body>

JS snippet

var app = angular.module('myApp', []);

app.directive('directive1', function() {
    return {
        restrict: 'A',

        replace: true,
        template: '<span>{{bar}}</span>'
    }

    });

app.directive('directive2', function() {
  return {
      restrict: 'A',
      scope:{


      },
       replace: true,
       link:function(s,e,a){
         s.bar = "Changed value";
       },
       template: '<b>{{bar}}</b>'
  }

});

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.bar ="original value";
});

How do i change the value of bar from directive2 so that it is reflected in directive1

If i make the scope:false it is happening.
Is there any other way, to make this happen.(because in the code i am writting , i cannot make scope:false).
My basic requirement is to make one directive to talk to another.
Here you can try the plunkr version of the below code

HTML snippet

<body ng-controller="MainCtrl">
  this is directive1: <div directive1></div>.<br/>
  this is directive2: <div directive2></div>.
</body>

JS snippet

var app = angular.module('myApp', []);

app.directive('directive1', function() {
    return {
        restrict: 'A',

        replace: true,
        template: '<span>{{bar}}</span>'
    }

    });

app.directive('directive2', function() {
  return {
      restrict: 'A',
      scope:{


      },
       replace: true,
       link:function(s,e,a){
         s.bar = "Changed value";
       },
       template: '<b>{{bar}}</b>'
  }

});

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.bar ="original value";
});
Share Improve this question edited Jul 19, 2015 at 11:05 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Apr 25, 2015 at 17:47 dreamerdreamer 9012 gold badges17 silver badges38 bronze badges 5
  • The second directive should use require to get a reference to the first directive's controller. docs.angularjs/api/ng/service/$pile#-require- – JB Nizet Commented Apr 25, 2015 at 17:56
  • @JBNizet but those two directive should be nested – Pankaj Parkar Commented Apr 25, 2015 at 17:57
  • 1 That's precisely what require is for. – JB Nizet Commented Apr 25, 2015 at 17:58
  • @dreamer could you please update your plunkr with code? – Pankaj Parkar Commented Apr 25, 2015 at 17:59
  • I have already read, that the directives should be nested, if require is to be used. So i know that. But in this scenario, which is a pletly valid situation, how do i get what i want – dreamer Commented Apr 25, 2015 at 18:00
Add a ment  | 

3 Answers 3

Reset to default 3

You could simply use bar inside your isolate scope, that will do two way binding with your variable which is assigned to bar attribute, That means change inside a directive on bar variable will reflect the changes on controller scope variable.

Markup

<body ng-controller="MainCtrl">
  this is directive1: <div directive1></div>.
  <br />
  this is directive2: <div directive2 bar="bar"></div>.
</body>

Directive

app.directive('directive2', function() {
  return {
      restrict: 'A',
      scope:{
        'bar': '=' //<-- Change here
      },
       replace: true,
       link:function(s,e,a){
         s.bar = "Changed value";
       },
       template: '<b>{{bar}}</b>'
  }

});

Working Plunkr

I have modified your code in here

Share the variable in both directives by passing it as '=' in the scope, changing it in one directive will change it in the the other.

<body ng-controller="MainCtrl">
  this is directive1: <div directive1 bar="bar"></div>.
  <br />
  this is directive2: <div directive2 bar="bar"></div>.
</body>

var app = angular.module('myApp', []);
app.directive('directive1', function() {
return {
    restrict: 'A',
    scope:{bar:'='},
    replace: true,
    template: '<span>{{bar}}</span>'
}

});

app.directive('directive2', function() {
  return {
    restrict: 'A',
    scope:{bar:'='},
    replace: true,
    link:function(s,e,a){
      s.bar = "Changed value";
    },
   template: '<b>{{bar}}</b>'
}

});

app.controller('MainCtrl', function($scope) {
 $scope.name = 'World';
 $scope.bar ="original value";
});

If they don't share the same $scope, the only way is using angularjs events.

In first directive you put:

$rootScope.$on('myEvent', function (event, data) {
  $scope.bar = data.bar;
});

In the second one when bar change:

$scope.$emit('myEvent', {bar: bar});

Taking into account that are directives pletely unrelated.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745635518a4637369.html

相关推荐

  • 电脑分区后进不了系统怎么办,修复教程

    电脑分区后进不了系统&#xff0c;可能的原因有多种&#xff0c;包括分区操作不当导致系统文件丢失或损坏、BIOS设置错误、引导文件未正确配置等。针对这些问题&#xff0c;可以尝试以下解决方法&#xff1

    1小时前
    00
  • 计算机开机长鸣报警,电脑开机报警,详细教您电脑开机一直长鸣报警怎么办

    朋友们在使用电脑的过程中&#xff0c;经常会碰到开机一声长鸣&#xff0c;然后一直响个不停&#xff0c;这到底是怎么回事呢? 其实这个现象的产生一般是因为内存的问题&#xff0c;那用户遇到电脑开机一直

    1小时前
    00
  • UMIT:统一多模态多任务视觉

    随着深度学习的迅速发展,尤其是在医学影像分析领域的应用,越来越多的视觉-语言模型(VLMs)被广泛应用于解决复杂的健康和生物医学挑战。然而,现有研究主要集中在特定任务或单一模态上,这限制了它们在多种医学场景中的适用性和泛化能力。为了解决这

    1小时前
    00
  • 取消Win10开机系统选择倒计时,让电脑秒进系统

    取消Win10开机系统选择倒计时,让电脑秒进系统 近期,不少Win10用户反映在开机时会遇到一个选择系统的倒计时画面,这在一定程度上延缓了开机进程。对于追求高效启动体验的用户来说,这无疑是一个不必要的步骤。那么,如何取消这个倒计时,让电脑

    1小时前
    00
  • 2025年最受欢迎的10款免费CRM软件大对比

    在数字化转型浪潮下,越来越多的企业开始重视客户关系管理(CRM)系统。一个高效的CRM不仅能帮助企业理清客户脉络,还能提升销售效率、优化服务体验。2025年,市场上涌现了众多优秀的免费CRM软件,本文将为大家对比10款最受欢迎的产品,助您选

    1小时前
    00
  • PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究

    全文链接:tecdat?p=41666在体育数据分析领域不断发展的当下,数据科学家们致力于挖掘数据背后的深层价值,为各行业提供更具洞察力的决策依据。近期,我们团队完成了一项极具意义的咨询项目,旨在通过先进的数据分析手段,深入探究篮球比赛中

    1小时前
    00
  • 聊聊Spring AI Alibaba的ObsidianDocumentReader

    序本文主要研究一下Spring AI Alibaba的ObsidianDocumentReaderObsidianDocumentReadercommunitydocument-readersspring-ai-alibaba-star

    1小时前
    00
  • PackML over OPC UA

    在当今数字化转型的浪潮中,制造业正面临着前所未有的挑战与机遇。如何实现设备之间的高效通信与集成,成为提升生产效率、降低成本的关键。OPC UA(OPC Unified Architecture)与PackML(Packaging Machi

    1小时前
    00
  • HLS最全知识库

    HLS最全知识库副标题-FPGA高层次综合HLS(二)-Vitis HLS知识库高层次综合(High-level Synthesis)简称HLS,指的是将高层次语言描述的逻辑结构,自动转换成低抽象级语言描述的电路模型的过程。对于AMD Xi

    58分钟前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    57分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    36分钟前
    00
  • CUT&amp;amp;Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    27分钟前
    10
  • 深度学习在DOM解析中的应用:自动识别页面关键内容区块

    爬虫代理摘要本文介绍了如何在爬取东方财富吧()财经新闻时,利用深度学习模型对 DOM 树中的内容区块进行自动识别和过滤,并将新闻标题、时间、正文等关键信息分类存储。文章聚焦爬虫整体性能瓶颈,通过指标对比、优化策略、压测数据及改进结果,展示了

    26分钟前
    10
  • 【Docker项目实战】使用Docker部署IT工具箱Team·IDE

    一、Team·IDE介绍1.1 Team·IDE简介Team IDE 是一款集成多种数据库(如 MySQL、Oracle、金仓、达梦、神通等)与分布式系统组件(如 Redis、Zookeeper、Kafka、Elasticsearch)管理

    18分钟前
    00
  • 重装系统只影响C盘吗?深入解析系统重装的全过程

    重装系统只影响C盘吗?深入解析系统重装的全过程 在计算机的日常使用中,重装系统是一个常见的操作,尤其是在系统出现故障、感染病毒或需要优化系统性能时。然而,许多用户对于重装系统的具体过程和影响存在误解,认为重装系统仅仅是对C盘进行清空和重置

    18分钟前
    10
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    17分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    10分钟前
    00
  • 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版

    文件: 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版 install.esd 大小: 2920270404 字节 修改时间: 2025年4月8日, 星期二, 11 : 04 : 59 MD5: D5F8F0AD

    7分钟前
    00
  • 人工智能适合什么人学

    一、引言:人工智能浪潮下的新机遇在当今科技飞速发展的时代,人工智能(AI)无疑是最为耀眼的技术明星之一。从智能语音助手到自动驾驶汽车,从医疗诊断辅助到金融风险预测,人工智能正以前所未有的速度改变着我们的生活和工作方式。随着全球领先的终身学习

    5分钟前
    00
  • ​2025 轻松部署 Odoo 18 社区版

    随着 Odoo 18 社区版的发布,越来越多的企业希望借助这款开源 ERP 系统实现数字化转型。本文将深入解析传统部署方式的底层逻辑,并揭示如何通过自动化工具实现零门槛快速部署。一、手工部署 Odoo 18 技术全解 Docker 环境搭建

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信