Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does my ng-model really need to have a dot to avoid child $scope problems?

Tags:

According to https://github.com/angular/angular.js/wiki/Understanding-Scopes, it's a problem to try to data-bind to primitives attached to your $scope:

Scope inheritance is normally straightforward, and you often don't even need to know it is happening... until you try 2-way data binding (i.e., form elements, ng-model) to a primitive (e.g., number, string, boolean) defined on the parent scope from inside the child scope. It doesn't work the way most people expect it should work.

The recommendation is

This issue with primitives can be easily avoided by following the "best practice" of always have a '.' in your ng-models


Now, I have this very simple setup which violates these rules:

HTML:

<input type="text" ng-model="theText" /> <button ng-disabled="shouldDisable()">Button</button> 

JS:

function MyController($scope) {     $scope.theText = "";     $scope.shouldDisable = function () {          return $scope.theText.length >= 2;     }; } 

Is this really bad? Is this going to screw me over in some horrible way when I start trying to use child scopes, somehow?


Do I need to change it to something like

function MyController($scope) {     $scope.theText = { value: "" };     $scope.shouldDisable = function () {          return $scope.theText.value.length >= 2;     }; } 

and

<input type="text" ng-model="theText.value" /> <button ng-disabled="shouldDisable()">Button</button> 

so that I follow the best practice? What concrete example can you give me where the latter will save me from some horrible consequence that would be present in the former?

like image 623
Domenic Avatar asked Jun 18 '13 21:06

Domenic


People also ask

What is difference between ng-model and Ng bind?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

Why do we use NG-model?

The ng-model attribute is used for,Binding controls such as input, text area and selects in the view into the model. Provide a validation behavior – for example, a validation can be added to a text box that only numeric characters can be entered into the text box.

How many child scopes can an AngularJS application have?

Scope Hierarchies Every angularJS application can have only one root scope as it is the parent scope. But it can have more than one child scope. New scopes can be created by directives which are added to parent scope as child scope.

How do NG models work?

NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.


1 Answers

A lot of things introduce new scopes. Let's say that in your controllers, you actually want to add tabs : first tab is actual rendering, second tab is the form (so that you have a real-time preview).

You decide to use a directive for that :

<tabs>   <tab name="view">     <pre>{{theText|formatInSomeWay}}</pre>   </tab>   <tab name="edit" focus="true">     <input type="text" ng-model="theText" />   </tab> </tabs> 

Well, know what ? <tabs> has its own scope, and broke your controller one ! So when you edit, angular will do something like this in js :

$scope.theText = element.val(); 

which will not traverse the prototype chain to try and set theText on parents.

EDIT : just to be clear, I'm only using "tabs" as an example. When I say "A lot of things introduce a new scope", I mean it : ng-include, ng-view, ng-switch, ng-controller (of course), etc.

So : this might not be needed as of right now, because you don't yet have child scopes in that view, but you don't know whether you're gonna add child templates or not, which might eventually modify theText themselves, causing the problem. To future proof your design, always follow the rule, and you'll have no surprise then ;).

like image 109
Ven Avatar answered Sep 24 '22 06:09

Ven