I am building an SPA using AngularJS and Bootstrap. I have a form which has a "Back" link on the navigation bar and 2 buttons (Save and Cancel). When the form is launched I want to display only the "Back" link (Save and Cancel should be hidden). However when the user starts inputting values into any of the form fields I want to hide the "Back" link & the Save & Cancel buttons should be shown to the user.
My controller logic is as shown below:
app.controller('TestController', function ($scope, $stateParams, testService) {
debugger;
$scope.id = $stateParams.id;
$scope.modeldata = null;
testService.getDetails($scope.id).success(function (data) {
$scope.modeldata = data;
$scope.beforeImage = angular.copy(data);
});
$scope.isUnchanged = function(formData){
debugger;
return angular.equals(formData, $scope.beforeImage);
};
....
I have the isUnchanged function that checks the data between the current state of the form and the initial state of the form. This function is then used in the actual ng-show / ng-hide directive as shown below:
<script type="text/ng-template" id="/testDetails.html">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-left">
<a ui-sref="TestList" class="btn-link btn navbar-btn y-back" ng-show="isUnchanged(modeldata)">
<span class="caret left"></span>
Back
</a>
</div>
<div class="navbar-right">
<button ng-click="cancel()" class="btn btn-default navbar-btn" ng-hide="isUnchanged(modeldata)">Cancel</button>
<button ng-click="updateTest()" class="btn btn-warning navbar-btn" ng-hide="isUnchanged(modeldata)">Save</button>
</div>
</div>
</div>
<form class="form-horizontal" role="form">
<legend>Test Details</legend>
<div class="col-sm-12">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">Basic Data</div>
<div class="panel-body">
<div class="col-sm-6">
<div class="form-group">
<label class="col-sm-2 control-label">Id</label>
<div class="col-sm-10">
<p class="form-control-static">{{ modeldata.testId }}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="testName">Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="testName" ng-model="modeldata.name" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="testDescription">Description</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="testDescription" ng-model="modeldata.description">
</div>
</div>
.....
Initial launch of the partial template correctly shows only the "Back" link and hides the Save & Cancel buttons. However when values are input into the editable form fields the switch does not happen.
Any ideas as to what could be going wrong ?