Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass function in ng-model

Is it posible to pass function into ng-model, for example

<input type="text" name="email" class="form-control" ng-model="createModel('email')" ng-change="addProperty(email,'email')" email required placeholder="Email">

ng-change is working fine, but ng-model="createModel(email)" is showing this error

> Expression 'createModel('email')' is non-assignable. Element: <input
> type="text" name="email"....

In controler i have : // I just want to pass value for now

  $scope.createModel = function(modelName){
     console.log("Model name"+modelName);
  }

I saw examples on the internet that people doing this

like image 220
Markuz Shultz Avatar asked Mar 13 '14 08:03

Markuz Shultz


People also ask

Can we pass function to ngModel?

Yeah, you can use a function. In which case the ng-model will be the value returned by the function.

What does [( ngModel )] mean?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.

What is the difference between ng-model and data NG model?

There is no difference between ng-model and data-ng-model if you see in terms of AngularJs. Actually, 'data' used as prefix to validate HTML5 validation. So, it is good practice to use data-ng-model , however, you can use ng-model as well. There is no problem in that also.

What is difference between ng-model and Ng bind directive?

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.


2 Answers

Looks like AngularJS added "getter" "setter" support in version 1.3

You can scroll to the bottom of their ngModel documentation page at:

https://docs.angularjs.org/api/ng/directive/ngModel

This allows you to specify a method instead of a variable in your ngModel attribute. The method should take an optional parameter. If an argument is passed it should store that value, if no argument is passed it should return a value.

You can see an example in another Stack Overflow answer at: https://stackoverflow.com/a/28224980/984780

like image 85
Luis Perez Avatar answered Oct 09 '22 01:10

Luis Perez


It's not possible to pass a function to ng-model because Angular has to be able to set the value when the user changes the input value. You cannot tell Angular to instead call a function when the value is changed. What you can do is define a property on the scope with a getter and setter method, something like:

var email = '[email protected]';
Object.defineProperty($scope, 'email', {
  get: function() {
    return email;
  },
  set: function(value) {
    email = value;
  }
});

But I'd say that you're better of creating a $watch for the property as that will be more familiar to other Angular devs.

EDIT: To bind to different models depending on other values, you'd still bind to the same property in ng-model, but you can swap that out in a watch. Something like this:

var model1 = {
  value: 'hi'
};
var model2 = {
  value: 'hello'
};
$scope.model = model1;

$scope.checkboxValue = true;
$scope.$watch('checkboxValue', function(value) {
  if (value) {
    $scope.model = model1;
  } else {
    $scope.model = model2;
  }
});

And:

<input type="text" ng-model="model.value">
<input type="checkbox" ng-model="checkboxValue">

That will change the value of your text input depending on if the checkbox is checked or not.

like image 45
Anders Ekdahl Avatar answered Oct 09 '22 03:10

Anders Ekdahl