See my FIDDLE
If I click the 'Click Me' button, my text fields are filled in. If I then click 'Add New Item', they are reset to blank. This is what I want to happen.
However, if I enter any text into the text fields before I click any buttons, the text fields do not change to reflect the GlossaryItem object in the scope. However I can see that the GlossaryItem value is the expected value when the 'debugger;' line is hit (so GlossaryItem and the textbox values are no longer the same).
I don't want to use $scope.$apply if it can be avoided (would that fix my issue?).
<div ng-app="editGlossaryApp">
<div ng-controller="MainCtrl">
<div>
<button ng-click="addNewDefinition()">Add New Item</button>
</div>
<div>
<label>Term:</label>
<div>
<input value="{{GlossaryItem.Term}}" />
</div>
</div>
<div>
<label>Definition:</label>
<div>
<textarea rows="16" cols="60">{{GlossaryItem.Definition}}</textarea>
</div>
</div>
<div>
<button ng-click="fill()">Click Me</button>
</div>
</div>
</div>
var app = angular.module('editGlossaryApp', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.GlossaryItem = {
Id: 0,
Term: '',
Definition: ''
};
$scope.addNewDefinition = function () {
debugger;
$scope.GlossaryItem = {
Id: 0,
Term: '',
Definition: ''
};
};
$scope.fill = function(){
$scope.GlossaryItem = {
Id: 1,
Term: 'Test',
Definition: 'Definition Definition Definition Definition Definition Definition'
};
}
}]);