Answer the question
In order to leave comments, you need to log in
How to get the value of the currently mutable array element?
There is a controller:
App.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
$scope.sortFunc = function (a, b) {
if (a.id> b.id) {
return 1;
}
if (a.id< b.id) {
return -1;
}
return 0;
};
$scope._url = "";
$scope._login = "";
$scope._password = "";
$scope.data = [];
/*
$scope.data = [
{id: 1, url: "http://example.org", login: "user", password: "12345"},
{id: 2, url: "http://example2.org", login: "user2", password: "12345"},
...
]
*/
if(localStorage["data"]){
$scope.data = JSON.parse(localStorage["data"]);
$scope.data.sort($scope.sortFunc);
}
$scope.Delete = function(id){
if(!id){
return;
}
var idObject = $scope.data.find({"id": id});
if(!idObject){
return;
}
$scope.data = $scope.data.exclude({"id": id});
idObject = null;
console.log("Delete item with id:", id);
}
$scope.Add = function(){
var tempArr = $scope.data;
tempArr = tempArr.sort($scope.sortFunc);
var inc = 1;
if(tempArr.length > 0){
var lastItem = tempArr.last();
inc = lastItem.id+1;
}
$scope.data.push({
id: inc,
url: $scope._url,
login: $scope._login,
password: $scope._password
});
$scope.data.sort($scope.sortFunc);
$scope._url = "";
$scope._login = "";
$scope._password = "";
console.log("Add item with id:", inc);
}
$scope.$watch(
'data',
function(newVal, oldVal, scope){
if(newVal.length < 0){
return;
}
localStorage["data"] = JSON.stringify($scope.data);
console.log(newVal);
},
true
);
}]);
Answer the question
In order to leave comments, you need to log in
Refactoring time!
$scope._url = "";
$scope._login = "";
$scope._password = "";
Do you know that there is a controllerAs that is recommended according to the angular styleguide and all the other existing best practices? (an explanation of why not to use it is read in the same angular-styleguide).
$scope.sortFunc = function (a, b) {
if (a.id> b.id) {
return 1;
}
if (a.id< b.id) {
return -1;
}
return 0;
};
function AppCtrl(orderByFilter) {
this.foo = orderByFilter(collection, 'id');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question