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
Yeah, you can use a function. In which case the ng-model will be the value returned by the function.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With