javascript - How can I bind ngModel through multiple directives with isolate scopes in AngularJs? - Stack Overflow

I am trying to bind to a property via ngModel in a layer of directives 3 levels deep. This would be fin

I am trying to bind to a property via ngModel in a layer of directives 3 levels deep. This would be fine, except the middle level contains a ng-if which I believe creates a new scope. The binding is lost at this point.

I have created a jsfiddle to explain the situation: /

Note that it works if the ng-if directive is removed, but I am using ng-if instead of ng-show for performance reasons

Does anyone know how I can get the original ngModel to update from the 'inputDirective' template in the fiddle?

I am trying to bind to a property via ngModel in a layer of directives 3 levels deep. This would be fine, except the middle level contains a ng-if which I believe creates a new scope. The binding is lost at this point.

I have created a jsfiddle to explain the situation: http://jsfiddle/5fmck/2/

Note that it works if the ng-if directive is removed, but I am using ng-if instead of ng-show for performance reasons

Does anyone know how I can get the original ngModel to update from the 'inputDirective' template in the fiddle?

Share Improve this question asked Apr 9, 2014 at 4:52 Erin DrummondErin Drummond 5,5266 gold badges36 silver badges41 bronze badges 1
  • Interesting question. Went to play jsfiddle – Miraage Commented Apr 9, 2014 at 5:04
Add a ment  | 

2 Answers 2

Reset to default 5

Simple :3

Just remember, that child scope is created = use reference to $parent :)

<div ng-if='someCondition'>
    <span>In Wrapper</span>
    <input-directive ng-model='$parent.ngModel'></input-directive>
</div>

http://jsfiddle/5fmck/3/

// upd

As I know you need to use reference to $parent only if ngModel is primitive, not object.

I suspect this is due to the nature of how scopes inherit from each other, and so you should use objects on the scope, and not primitives, and always pass the objects into directives via attributes if it's to be then to be used in another scope. So instead of:

$scope.test = "test";

and

<wrapper-directive ng-model="test" some-condition="true">

use:

$scope.userInput = {
  test: "Test"
}

and

<wrapper-directive user-input="userInput" some-condition="true"> 

Which can be seen at http://jsfiddle/4RBaN/1/ (I've also changed all-but-one of the ngModels to another custom attribute, as if you're not using ngModel specific things, like ngModelController, or integration with ngForm, then I think better to KISS).

The reason is that if you have 2 scopes in a parent/child relationship (via prototypical inheritance), such as the one created by ngIf, if you execute (or if Angular executes):

$parentScope.test = 'test';
$childScope.test = 'My new value';

Then $parentScope.test will still equal 'test'. However, if you use objects:

$parentScope.userInput = {test: 'test'}
$childScope.userInput.test = 'My new value';

Then $parentScope.userInput.test will then equal 'My new value'.

The standard quote from (whom I believe was) the original author of Angular, is "If you haven't got a dot in your model, you're doing it wrong". There are other questions on SO about this, such as If you are not using a .(dot) in your AngularJS models you are doing it wrong?

Edit: If you do want to use ng-model, and always pass a primitive to the directive, and not an object, then there is a way that allows this. You'll need to:

  • Always ensure there is a dot in the model that is passed to ngModel, in every directive. Each directive therefore needs to create a new object on its scope in its controller (or linking function). So the template for your 'middle' directive will be:

    <input-directive ng-model='userInput.test'>
    

    And in its controller:

    $scope.userInput = {
      test: $scope.ngModel   
    }
    

    Where ngModel is bound to the value specified in the directive's attributes:

    "scope" : {
      "ngModel" : "="
    }
    
  • Set up manual watchers in each directive, so that changes to the model from the inner directives get propagated back up the chain:

    $scope.$watch('userInput.test', function(newTest) {
      $scope.ngModel = newTest;    
    });
    

This can be seen at: http://jsfiddle/zT9sD/ . I have to say, it feels a bit plicated, so I'm not quite sure I can remend it. Still, (in my opinion) better than using $parent, as introducing a new scope can quite easily happen as things get more plicated, and then you'll have to use things like $parent.$parent.

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

相关推荐

  • Windows下配置Golang开发环境,并安装配置GoLand IDE

    作者&#xff1a;非妃是公主 专栏&#xff1a;《Golang》 博客地址&#xff1a;https:blog.csdnmyf_666 个性签&#xff1a;顺境不惰&#xff0c;逆境不馁

    1小时前
    00
  • windows 配置 upx

    ​1、下载:2、解压:解压后​3、配置环境变量,右键我的电脑-——》属性——》高级属性:在Path中添加: D:ProgramFileupx-5.0.0-win644、验证cmd中输入:代码语言:txt复制upx --version

    1小时前
    20
  • 文章降 AI 痕迹方法与工具速览

    文章降AI的方法和工具汇总‌如下: 这几天又认真研究类了一下,想让 AI 生成的文章更自然,摆脱程式化痕迹,可尝试以下方法。借助 GPT、文字滚筒鸭,朱雀大模型检测器、豆包、kimi 等大模型,输入文本后,它们能通过调整结构、替换同义词等操

    1小时前
    00
  • MySQL 8.4 配置SSL组复制(八个步骤)

    环境这里有三台MySQL主机,分别是192.168.3.71,72,73,主机名分别对应71.3_mgr1,72.3_mgr2,73.3_mgr3,操作系统均为Oracle Linux 8.10 X64,MySQL版本均为MySQL 8.4

    1小时前
    00
  • 利用小提琴图探索帕尔默企鹅数据

    利用小提琴图探索帕尔默企鹅数据代码语言:javascript代码运行次数:0运行复制import numpy as npimport matplotlib.pyplot as pltimport scipy.stats as stfro

    1小时前
    00
  • MySQL 8.4 配置复制

    参考文档:.4enreplication-configuration.html1.先在源数据库主机的myf添加这几项代码语言:javascript代码运行次数:0运行复制[mysqld]server-id = 2binlog_forma

    1小时前
    00
  • Oracle linux 8 二进制安装 MySQL 8.4企业版

    使用命令ldd --version ldd 检查,确定MySQL 8二进制包版本代码语言:javascript代码运行次数:0运行复制[root@mysql8_3 ~]# ldd --version ldd安装libaio代码语言:java

    1小时前
    00
  • AI生态暗战升级,科技巨头铁幕下的终极博弈

    一场围绕AI与智能体的标准、协议及生态的暗战已然蓄势待发。在美剧《权力的游戏》中,不到终局,主角归属始终成谜。如今的AI行业,正上演着同样扣人心弦的戏码。这并非是传统意义上的军事或政治博弈,而是一场围绕AI与智能体的标准、协议及生态展开的暗

    1小时前
    00
  • Go项目实战

    上节课我给大家介绍了怎么给Go项目做单元测试的规划,当然这里仅限于跟咱们课程里的实战项目一样分层架构设计做的还可以的项目哦,要是所有逻辑都耦合在Controller里,那这个规划就不适用了。。。,所有逻辑都耦合在Controller里还做个

    57分钟前
    00
  • 计算机开机长鸣报警,电脑开机报警,详细教您电脑开机一直长鸣报警怎么办

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

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

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

    54分钟前
    00
  • 聊聊Spring AI Alibaba的ObsidianDocumentReader

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

    50分钟前
    00
  • PackML over OPC UA

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

    48分钟前
    00
  • 初始JESD204B高速接口协议(JESD204B一)

    01、对比LVDS与JESD204JESD204B是逻辑器件和高速ADCDAC通信的一个串行接口协议,在此之前,ADCDAC与逻辑器件交互的接口大致分为如下几种。低速串行接口(I2C、SPI)、低速并行接口(包含时钟信号和并行数据信号,

    42分钟前
    00
  • HLS最全知识库

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

    40分钟前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    37分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    25分钟前
    00
  • 最简 Odoo 部署方法:Websoft9 企业应用托管平台

    传统方式部署 Odoo 通常依赖 Docker 技术,主要分为以下步骤:1 . 安装 Docker需在服务器上安装 Docker 引擎,涉及操作系统兼容性检查、依赖包安装、镜像源配置等操作。代码语言:bash复制 # 以 Ubu

    22分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

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

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

    8分钟前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信