Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you log angular data-binding errors?

Tags:

angularjs

is there anyway to log if a bound property or expression fails?

i.e.

<input type="text" ng-model="user.name" />

Log when user or name is undefined?

Edit: There seems to be a lot of confusion about how this could happen. Lets assume I use a viewmodel for multiple views (Or I'm very forgetful)

Imaginge that I change the JS code so that name is now user.firstName and I forget to update my view. I would like to have it logged at runtime so I can fix it.

like image 694
Adam Mills Avatar asked May 10 '13 18:05

Adam Mills


People also ask

What is data binding in Angular with example?

Two-way data binding is a two-way interaction, data flows in both ways (from component to views and views to component). Simple example is ngModel. If you do any changes in your property (or model) then, it reflects in your view and vice versa. It is the combination of property and event binding.

What is 2 way data binding in Angular?

The two-way data binding in Angular is used to display information to the end user and allows the end user to make changes to the underlying data using the UI. This makes a two-way connection between the view (the template) and the component class. This is similar to the two-way binding in WPF.

How many types of data binding are there?

AngularJS provides two types of Data Binding: One-way data binding. Two-way data binding.


1 Answers

As others have mentioned in the comments, the data binding won't "fail" per se when the attribute is not defined in the scope, but will create that attribute on the scope transparently.

If you want some notification behaviour when the name isn't found, you can get it manually by decorating the ng-model directive to check if its value is defined on the scope at the time it's inserted into the DOM.

.config(['$provide', function($provide) {
        $provide.decorator('ngModelDirective', ['$delegate', function($delegate){
            var directive = $delegate[0];

            // Save the old link function
            var link = directive.link;
            directive.compile = function() {
                return function(scope, element, attrs) {
                    link.apply(this, arguments);

                    // Now that we've applied the old link function, we can add
                    // any extra checks or steps we want
                    if (!objHasProperty(scope, attrs.ngModel)) {
                        alert("using ng-model value '" + attrs.ngModel +"' that wasn't defined first!"
                    }
                };
            };

            return $delegate;
        }]);
    }])

This will check for a definition of the ng-model value in the controller's scope and alert if it's not set.

See a working jsfiddle of how this might log a typo.

I haven't tested this in or thought about every scenario, so it's possible it's really broken somewhere... I'm also unsure of how it'll deal with finding attrs that are defined in the parent's scope.

Also, see this nice blog post for more on decorating directives.

like image 128
crennie Avatar answered Nov 05 '22 01:11

crennie