Someone please give me an idea how to solve this. In my main file I use startEdit($index) function that grabs the input value, that has to be changed, and redirect me to /edit partial. Here I type a new text in the input box and use save() to save the new value and then redirect me back to main view, where I can see the new text.
The problem is that I don't get in the save() function the new value for $rootScope.siteBox; . I get the same old value, even in the $watch function I can see that the variable is changing.
I tried a lot of solutions but with no result. Someone please give a hand and tell me what is wrong.
JS FILE
angular.module('maintenance', ['ngRoute'])
.controller('siteEditCtrl', SiteEditCtrl)
.controller('mainCtrl', MainCtrl)
.controller('addCtrl', AddCtrl)
.controller('editCtrl', EditCtrl)
.controller('deteleCtrl', DeteleCtrl)
.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'views/lists.html',
controller: 'mainCtrl'
})
.when('/add', {
templateUrl: 'views/add.html',
controller: 'addCtrl'
})
.when('/edit', {
templateUrl: 'views/edit.html',
controller: 'editCtrl'
})
.when('/detele', {
templateUrl: 'views/detele.html',
controller: 'deteleCtrl'
})
.otherwise({
redirectTo: '/'
});
});
function SiteEditCtrl($scope, $location, $rootScope) {
$rootScope.sites = sites;
$rootScope.selected = -1;
$scope.startAdd = function() {
$location.path( "/add" );
};
$scope.startEdit = function(index) {
$rootScope.selected = index;
$rootScope.siteBox = $rootScope.sites[index];
//console.log($rootScope.siteBox);
$location.path( "/edit" );
};
}
function EditCtrl($scope, $location, $rootScope) {
$scope.$watch('siteBox', function(newValue, oldValue) {
//console.log(oldValue);
//console.log(newValue);
});
$scope.save = function() {
console.log($rootScope.siteBox);
$rootScope.sites[$rootScope.selected] = $rootScope.siteBox;
$location.path( "#/" );
};
}
Main view in config(/)
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary btn-lg" ng-click="startAdd()">
Add new dive
</a>
</div>
</div>
<h2>List of Dive Sites</h2>
<div class="row" ng-repeat="site in sites"
ng-class="{oddRow: $index % 2 == 0}">
<div class="col-sm-8">
<h4>{{$index + 1}}: {{site}}</h4>
</div>
<div class="col-sm-4" style="margin-top: 5px;">
<div class="pull-right">
<button class="btn btn-warning btn-sm" ng-click="startEdit($index)">
Edit
</button>
<button class="btn btn-danger btn-sm" ng-click="startRemove($index)">
Delete
</button>
</div>
</div>
</div>
Edit view in config(/edit)
<h3>Edit the dive site name</h3>
<div class="row">
<div class="col-sm-6">
<input type="text" class="form-control input-lg" placeholder="site name" ng-model="siteBox">
</div>
</div>
<div class="row" style="margin-top: 12px;">
<div class="col-sm-6">
<button class="btn btn-succes btn" ng-disabled="siteBox==''" ng-click="save()">
Save
</button>
<button class="btn btn-warning btn" ng-click="cancel()">
Cancel
</button>
</div>
</div>