Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from ngModel in directive when using object with property

Tags:

angularjs

Not sure how to phrase the question so please edit if you can come up with something better. I have the following directive:

app.directive('foo', function() {
    return {        
        restrict: 'A',
        require: "?ngModel",
        link: function (scope, element, attrs, controller) {
            scope.$watch(attrs.ngModel, function () {
                console.log("Changed to " + scope[attrs.ngModel]);
            });
        }
    };
});

When I have this it works great and logs properly

<input type="text" ng-model="bar" />

app.controller('fooController', function($scope) { 
    $scope.bar = 'ice cream';
});

It doesn't work when I try it this way around. It keeps logging 'Changed to undefined'

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

app.controller('fooController', function($scope) { 
    $scope.model = { bar: 'ice cream' };
});

How do I make it work for both scenarios. It seems the right thing to do seeing as angular lets you use both.

like image 846
uriDium Avatar asked Nov 14 '13 07:11

uriDium


People also ask

How to create a form using ngmodel directive in angular?

Angular forms NgModel Directive. 1 Create the Angular app to be used. 2 In app.component.ts make a variable that gives value to the input field. 3 In app.component.html make a form and use ngModel to get the value of the input. 4 Serve the angular app using ng serve to see the output.

What is the use of ngmodel?

The ngmodel directive binds the value of HTML controls (input, select, textarea) to application data. With the ng-model directive you can bind the value of an input field to a variable created in Angular. The binding goes both ways. If the user changes the value inside the input field, the Angular property will also change its value.

Can We pre-define the value of an ngmodel in typescript?

We can also pre-define value that property in typescript so that it will display the event value when loading. The ngModel is a fundamental directive for creating user-friendly forms with the template-driven approach; it creates a FormControl instance from a domain model and connects with a form control element.

How to access form values in form submit using ngmodel?

On form submit, the form values can be accessed in class as given below. If we use neither one-way binding not two-way binding still we need to use ngModel attribute in fields when using with parent form as given below. Then this field will not be registered with parent form and its value will not be passed to our class on form submit.


2 Answers

I looked at ngModel directive and found a function called ngModelGet. Uses $parse.

app.directive('foo', function($parse) {
    return {        
        restrict: 'A',
        require: "?ngModel",
        link: function (scope, element, attrs, controller) {
            var ngModelGet = $parse(attrs.ngModel);

            scope.$watch(attrs.ngModel, function () {
                console.log("Changed to " + ngModelGet(scope));
            });
        }
    };
});
like image 173
uriDium Avatar answered Nov 05 '22 05:11

uriDium


your can use

var ngModelCtrl = controller;
ngModelCtrl.$viewValue

replace

scope[attrs.ngModel]

here is ngModelCtrl sdk

like image 35
Kain Avatar answered Nov 05 '22 05:11

Kain