Why is ngModel non-assignable

Meinhh picture Meinhh · Oct 18, 2015 · Viewed 10.3k times · Source

I have a date and i want it to have a filter on it so it would have the format i want for it.

html:

<input type="text" id="{{ID}}" ng-style="{color: color}" class="tdate ng-pristine ng-untouched ng-valid" ng-model="time.date | DatePlaceHolder">

filter:

function DatePlaceHolder($filter) {
    return function (value) {
        return $filter('date')(value, "dd/MM/yyyy") || "--/--/----";
    }
}

It works fine but I keep getting this error:

Error: [ngModel: nonassign] Expression 'time.date | DatePlaceHolder' is non-assignable. Element: <input type="text" id="{{ID}}" ng-style="{color: color}" class="tdate ng-pristine ng-untouched ng-valid" ng-model="time.date | DatePlaceHolder">

Answer

Laxmikant Dange picture Laxmikant Dange · Oct 18, 2015

ng-model is to bind model with views to take input or display view. you cant use filter on ng-model.

If you want to display data only, then you can use ng-bind for non input elements, and ng-value for form elements.

Here is example.

$scope.someValue='Value in controller'