I have an Angular controller variable defining the look-and-feel of a bunch of fields which I want to render in a template. The initial value of my underlying model correctly appears in the input element, but it doesn't change when I change the content of that element.
Here is some code:
app.js
(function() {
var app = angular.module("my_app", []);
app.controller("MyController", function() {
var my_controller = this;
my_controller.model = {
"first_name": "fred",
"last_name": "jones"
}
my_controller.fields = [
{
"name": "first_name",
"label": "First Name",
"is_required": true,
"model": my_controller.model.first_name
},
{
"name": "last_name",
"label": "Last Name",
"is_required": true,
"model": my_controller.model.last_name
}
]
})();
template.html:
<div ng-app="my_app">
<div ng-controller="MyController as my_controller">
<div ng-repeat="field in my_controller.fields">
{% verbatim ng %}
<label for="id_{{ field.name }}" ng-class="field.is_required ? 'required' : 'optional'">{{ field.label }}: </label>
<input type="{{ field.type }}" class="form-control" name="{{ field.name }}" id="id_{{ field.name }}" ng-model="field.model">
{% endverbatim ng %}
</div>
</div>
</div>
(Don't worry about the "{% verbatim ng %}" stuff - that's just there b/c I'm using Django.)
This correctly displays two input fields with an initial value of "fred" and "jones" respectively. But when I change the value in those fields, there is no corresponding change to the my_controller.model object.
Any hints?
Thanks.
ng-repeatwhen your variablemy_controller.fieldsis not an array?user_controller? If your controller ismy_controller.user_controllerwas left over from the original code; This is obviously a very scaled-back example just to illustrate the problem.