V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
duxiaodong121
V2EX  ›  问与答

Angular2 双向绑定的问题

  •  
  •   duxiaodong121 · Sep 1, 2016 · 3244 views
    This topic created in 3535 days ago, the information mentioned may be changed or developed.
    <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 replies    2017-01-05 16:23:29 +08:00
    duxiaodong121
        1
    duxiaodong121  
    OP
       Sep 1, 2016
    根本看不懂 V2EX 怎么排序的
    WhoMercy
        2
    WhoMercy  
       Sep 1, 2016 via Android
    Angular2 貌似看不懂了( ̄∀ ̄)……
    crysislinux
        3
    crysislinux  
       Sep 1, 2016
    你应该让外部来改变 show 的值,而不是在内部改。
    xjp
        4
    xjp  
       Sep 1, 2016
    改两个地方
    1. <x-test [show]="isShow" (showChange)="isShow=$event"></x-test>
    2. @Output() showChange: EventEmitter<boolean> = new EventEmitter()
    xjp
        5
    xjp  
       Sep 1, 2016
    duxiaodong121
        6
    duxiaodong121  
    OP
       Jan 5, 2017
    @xjp 不是,我发的代码能工作,只是样子不好看,想写成 `[(show)]="isShow"` 的,简单的这样写是实现不了的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1141 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:10 · PVG 07:10 · LAX 16:10 · JFK 19:10
    ♥ Do have faith in what you're doing.