Say I have some user data that's been loaded. That's now stored in a 'user' model that's within mainCtrl's scope. I'd then like an user update form that is initially populated with this user data, but only updates the 'user' model when the form has successfully been submitted.
I'm I going about this entirely wrong? I'm running in to difficulties with references being assigned, so the two models ('user' and 'updateUser') cannot be updated independently.
HTML
<div ng-controller="mainCtrl">
<form ng-controller="updateCtrl" ng-submit='submit()'>
<input ng-model='updateUser.firstName'>
<input ng-model='updateUser.email'>
</form>
</div>
JS
var userData = {
'firstName': 'Chris',
'email': 'test@test.com'
}
app.controller('mainCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.user = userData;
};
]);
app.controller('updateCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.updateUser = $scope.user;
$scope.submit = function () {
// Submit form to server and on success, update $scope.user
}
};
]);