V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
duxiaodong121
V2EX  ›  问与答

Angular2 双向绑定的问题

  •  
  •   duxiaodong121 · 2016-09-01 18:34:54 +08:00 · 2670 次点击
    这是一个创建于 2785 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <x-test [show]="isShow" (show)="isShow=$event"></x-test>
    
    @Component({
    	selector: 'x-text'
    })
    class XText {
    	@Input('show') show: boolean = false
    	@Output('show') showChange: EventEmitter<boolean> = new EventEmitter()
    	
    	hide() {
    		this.show = false
    		this.showChange.emit(this.show)
    	}
    }
    

    请问上面代码要怎么改动, html 才可以写成 <x-test [(show)]="isShow"></x-test>

    6 条回复    2017-01-05 16:23:29 +08:00
    duxiaodong121
        1
    duxiaodong121  
    OP
       2016-09-01 18:39:06 +08:00
    根本看不懂 V2EX 怎么排序的
    WhoMercy
        2
    WhoMercy  
       2016-09-01 18:51:30 +08:00 via Android
    Angular2 貌似看不懂了( ̄∀ ̄)……
    crysislinux
        3
    crysislinux  
       2016-09-01 19:39:18 +08:00
    你应该让外部来改变 show 的值,而不是在内部改。
    xjp
        4
    xjp  
       2016-09-01 20:54:36 +08:00
    改两个地方
    1. <x-test [show]="isShow" (showChange)="isShow=$event"></x-test>
    2. @Output() showChange: EventEmitter<boolean> = new EventEmitter()
    xjp
        5
    xjp  
       2016-09-01 21:01:26 +08:00
    duxiaodong121
        6
    duxiaodong121  
    OP
       2017-01-05 16:23:29 +08:00
    @xjp 不是,我发的代码能工作,只是样子不好看,想写成 `[(show)]="isShow"` 的,简单的这样写是实现不了的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5878 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:21 · PVG 14:21 · LAX 23:21 · JFK 02:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.