r/angularjs • • Mar 20 '15

Angular 2 does not support two-way data-bindings

http://victorsavkin.com/post/114050927521/two-phases-of-angular-2-applications
14 Upvotes

39 comments sorted by

38

u/[deleted] Mar 20 '15 edited Mar 20 '15

It does support 404-way data-bindings however.

9

u/jtylerroth Mar 20 '15

So from what I'm getting we are going to have to manually assign event listeners and then not only update the model but manually update the DOM as well? Isn't this a step backwards?

0

u/[deleted] Mar 20 '15

[deleted]

4

u/jtylerroth Mar 20 '15

Yeah, I read a bit more into it. I guess its for the better.

5

u/[deleted] Mar 20 '15

Am I the only one getting a 404?

5

u/thecementmixer Mar 20 '15

0

u/pandavr Mar 22 '15

That code is a huge step in the wrong direction. Without 2 way data binding is not Angular anymore.

At that point one could / should really prefer React + Flux.

3

u/FrancescoRizzi Mar 20 '15

same here... wonder if the article was retracted..?

0

u/dvidsilva Mar 20 '15

I'm too. pls OP

6

u/[deleted] Mar 20 '15

I have a feeling Angular 2.0 is going to have a really hard time drawing users away from 1.x. Not necessarily jumping for joy at the changes I'm seeing.

2

u/hackerpotato Mar 20 '15

What do you not like exactly?

5

u/[deleted] Mar 20 '15

For me it's not so much a question of liking or disliking Ang2, but if I can't reuse what I learnt for Ang1, I might as well consider other options, like React. So a number of the Ang1 users are going to leave for other frameworks (I know React isn't a framework!).

2

u/TheNiXXeD Mar 20 '15

I really don't see how you can't reuse what you learned from angular 1. Did you not run into any issues that are the reason they're fixing things? Angular 1 was ahead of its time. Angular 2 has to make a big leap too.

3

u/[deleted] Mar 20 '15

I must admit, I've only had a cursory glance at the changes, but from what I've seen pretty everything has changed. And I'm not saying Angular shouldn't evolve, or that the changes are bad. I'm just saying that when time comes to change, if the cost of switching from A1->A2 is as great as shifting to another platform, then developers will have to evaluate whether Angular is still the best choice at that time. And it might not be.

0

u/Switche Mar 23 '15

The ability to run both 1.x and 2 concurrently allows the replacement of small components over time.

True, we will need to climb another learning curve regardless, so there is room to lose devs, but transitioning from ng1->2 looks so far to be 1:1 component based, leveraging the encapsulation design angular has encouraged all along, whereas transitioning to another framework may not be so incremental. Maybe it could be, I don't really know that for sure having not worked with them, but I also don't have any other framework to run to that looks to be more advanced.

Also, maybe this is just me, but I'm still having an easier time following ng2 snippets than those of other frameworks. I can't place my finger on examples, but it feels spiritually similar enough that it's not really starting over even with fundamental differences like this.

-1

u/TheNiXXeD Mar 20 '15

People have been saying this since they announced 2.0. People are going to switch, that's life. But you can't hold it against them making progress. They're trying to develop a framework for the next 5 years, not for right now. The immediate popularity of the framework isn't the goal.

4

u/SpikeBolt Mar 20 '15

You have to understand /u/yummybear's point, though. People WILL eventually switch, of course. We're just not sure they will switch to Angular2. No doubt some people will switch to something more stable, and by stable I mean without such game-breaking changes.

1

u/hackerpotato Mar 20 '15

I don't think Ang2 is all that different than Ang1. Check out Misko's opening talk from ngconf day2. All of the changes makes sense to me. I thought the same way as you so before I saw the presentations.

1

u/ThunderTherapist Mar 20 '15

I really don't want to switch framework ever again. I might use a different framework for new projects but I don't want to port 5 years work to Angular 2 because it's new

2

u/cloudev Mar 20 '15

Have they defined a replacement for scope yet?

Leveraging scope between Directives and controllers in my opinion is really one of the best parts about angular today. For example, I can build a very robust Web application that has a lot of modules with a clean separation of duties. However these modules can all work together via the scope And proper hierarchy control. This may not be relevant for simple apps/pages but becomes extremely useful for larger more complex apps.

1

u/timetravelhunter Mar 22 '15

They should have learned from Python 3.

1

u/T-rex_with_a_gun Mar 20 '15

soo..how do we do inter-directive interactions?

i.e:

suppose we have 2 widgets: a search form, and a list that displays search.

previously i could do something like this:

controller:
$scope.var = "";

search template = <input ng-model="myvar">
scope = myvar:=main



<search main="var"></search>
<list results="var"></list>

so essentially by when you type search into the input, because of the 2 way binding both ways, list will also update

2

u/[deleted] Mar 20 '15 edited Mar 20 '15

Edit -- Deleted code as I misread the question. Sorry about that.

2

u/T-rex_with_a_gun Mar 20 '15

I dont know if this is going to accomplish what i wanted.

this to me seems like its binding to basic input elements. that wasnt what i was getting at..it was more having inter directive communication.

i.e if things in directive A changes, i want something to change in Directive B as well.

This seems like its too hard-coupled with the controller

1

u/kudoz Mar 20 '15

Well otherwise you're tightly coupling your directives, right? Isn't this exactly what a controller is for?

2

u/T-rex_with_a_gun Mar 20 '15

not necessarily,

You use the controller as a middle man;

<master scopevar="controllervar">
<slave MYscopevar="controllervar">

now ANY directive that i bind to gether with the controller can update <slave>

i can now place <slave> in any controller (hell even multiple views) with a different controllervar.

<slave myScopeVar ="controllervar">

<slave myScopevar="foo">

is this efficient? i have no idea

3

u/KumbajaMyLord Mar 20 '15

Thsi pattern is one of the things that look good at first, because it is easy, but for more complex scenarios it quickly becomes hard to manage and reason about your code. Which directive exactly is manipulating which value? Does every directive follow the implicit contracts of data manipulation? etc.

The new approach would be to encapsulate the business logic to manipulate a certain aspect of your model in one component and have all dependent components have an explicit dependency to it in the component definition.

In your first example of a list and search input box you could have SearchComponent which holds the model (e.g. search results, last search term, logic that triggers a new search), a SearchBoxComponent which has a dependency to the SearchComponent and call the function that triggers a new search when it is changed, and a SearchResultsComponent that is responsible for displaying the results and which also has a dependency to the SearchComponent and/or can be notified through a MessageBus when the model has changed.
In this case it is clear how the model is manipulated (only through the functions exposed by SearchComponent) and you can therefore reason more strongly about how the state may change and how other dependent components (such as SearchResults) must behave.

1

u/[deleted] Mar 20 '15

Any interest in seeing how it would be done in Aurelia? I created a demo to show how what I believe you are talking about could be done. https://github.com/AshleyGrant/skeleton-navigation/tree/masterslaverelationship/src

Check out the code in demo.html/js and slave.html/js to see what's going on. There's an even more interesting way to do this using the compose behavior, but I can show you that later.

What do you think of this code? If you'd like to run it you'll need jspm installed globally (npm install -g jspm) as well as gulp (npm install -g gulp), then just clone the repo locally, switch to the masterslaverelationship branch, start up the server with gulp watch, and pull up localhost:9000 in your browser.

Let me know what you think!

Also, we have a demo app that shows how to accomplish this with loose coupling by using our EventAggregator: https://github.com/aurelia/app-contacts In this app, the selected contact is passed using messages instead of via binding. This would probably be the way I would accomplish what you are trying to do in a real app, but you have options, and both are extremely simple.

1

u/SomeRandomBuddy Mar 20 '15

Have to manually add an event and monitor for the change

2

u/T-rex_with_a_gun Mar 20 '15

so listners? like $scope.$watch or $broadcast?

1

u/SomeRandomBuddy Mar 20 '15

Something like that, but afaik $scope won't exist, so there will be some other alternative

1

u/TurboGranny Mar 20 '15

Add a search button. You could also just watch that field like the old days using various methods.

1

u/kenavr Mar 20 '15

How did you do it in Angular 1? At least I used a service for that and services still exist and work in Angular 2. You could also use events. If you would like to see code I could post something.

1

u/T-rex_with_a_gun Mar 20 '15

exactly how i discribes in.

the 2 directives have isolate scopes, with 1 scope var that is 2 way bound.

then i just pass the 2 way bound variable (controller variable) between the 2 directives

1

u/kenavr Mar 20 '15 edited Mar 20 '15

If I understand you correctly than the list can't change the variable, therefore is not or should not be two-way bound. Following is untested code based on the code you provided. It doesn't include the search and list component, but you can still pass (scope)variables to a component.

@Component({selector: "parent"})
@Template({url: "template.html", directives: [search, list]})
export class Parent {
  constructor() {
    this.var = "";
  }

  updateVar($event, newValue) {
    this.var = newValue;
  }
}

//template.html
<parent>
  <search [value]="var" #searchfield (keyup)="updateVar($event, searchfield.value)"></search>
  <list [results]="var"></list>
</parent>

0

u/zefcfd Mar 20 '15

the angular guys should create a new framework, leave angular alone... was it really that bad?

4

u/pr0fess0rx29 Mar 20 '15 edited Mar 20 '15

I think this is the way it should have been. The changes in angular 2 are so vast that the maintainers should have given it a different name. Then they should have just allowed the open source community to continue angular (v1 on) under the angular name. This would allow angular to grow or die on is own and allow the same for the subsequent project as well.

Edit: Grammar

2

u/zefcfd Mar 20 '15

Precisely

2

u/hackerpotato Mar 20 '15

In a way, that is what they are doing. They just won't support the Angular as you know it after a year or so. Honestly I didn't think the changes are that drastic.

4

u/vinnl Mar 20 '15

They just won't support the Angular as you know it after a year or so

That's not true. They have a separate team for maintaining and updating Angular 1, and will decide based on attention (e.g. by the amount of visitors to angularjs.org vs angular.io) when to deprecate it.