One way data binding in Angular2

前端 未结 4 1907
情歌与酒
情歌与酒 2020-12-08 08:39

I got the following html


I want the data and columns properties to

相关标签:
4条回答
  • 2020-12-08 09:08

    From Angular official documentation:

    0 讨论(0)
  • 2020-12-08 09:20

    Firstly, you need to understand what databinding means in Angular?

    What are Databindings ?

    "Data bindings are expressions embedded into templates and are evaluated to produce dynamic content in the HTML document. Data bindings provide the link between the HTML elements in the HTML document and in template files with the data and code in the application."(from Pro Angular book)

    [target]: expression

    The square brackets indicate a one-way data binding where data flows from the expression to the target. One-way databindings in only one direction, which data flows from component to html template file.

    {{expression}}

    String interpolation binding, that is used to include expression results in the text content of host elements.

    (target) ="expr"

    The round brackets indicate a one-way binding where the data flows from the target to the destination specified by the expression. This is the binding used to handle events.

    [(target)] ="expr"

    This combination of brackets—known as the banana-in-a-box—indicates a two-way binding, where data flows in both directions between the target and the destination specified by the expression.

    0 讨论(0)
  • 2020-12-08 09:21
    • if you use [ngModel], [value], {{ param }} etc. you have one-way binding, model to view,
    • if you use (ngModelChange) you have one-way binding, view to model,
    • if you use [(ngModel)] you have two way binding.

    But you are using a sub-component with the input @Input() property and this dances out of the line ;-) The notation is not what it looks like because it's always binded.

    <sub-component [prop]="myObj"></sub-component>
    

    So if you change the myObj in your sub-component, it will be binded:

    ngOnInit() {
        this.myObj = this.myObj.push(this.newObj);
    }
    

    You could work with a local copy of myObj to prevent binding.

    If you need an update from model you can push it with @Output() as Event:

    <sub-component [prop]="myObj" (update)="myObj = $event"></sub-component>
    
    0 讨论(0)
  • 2020-12-08 09:22

    You are right, syntax [target]=expression is a one way data binding, but I think that you have misunderstood the idea of the one way data binding.

    One way data binding from data source to the view target means that values from the view are not passed back to the component, while any changes made to the expression in the component are reflected in the view - it is one way data binding from data source to the view, what does not mean that it is one time one way data binding.

    On the other hand you may find one way data binding from the view target to data source with syntax (target)=expression which is used for passing events back to the components.

    You can find more about Angular2 data binding in the docs here: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#binding-syntax.

    0 讨论(0)
提交回复
热议问题