A
A
Alexey Yarkov2016-03-11 16:54:38
Angular
Alexey Yarkov, 2016-03-11 16:54:38

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
  );
  
}]);

In $scope.$watch newVal contains the $scope.data array. And accordingly, saving in localStorage occurs when any element of the array changes.
Can I somehow get the value of the currently modified object property in this array? I need to check if it is not empty, so as not to store empty values ​​in an array.
For more details, you can look at the source here: github
Temporary demo here: demo

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-03-11
Protko @Fesor

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;
  };

Do you know that Angular has a built-in orderBy filter?
function AppCtrl(orderByFilter) {
    this.foo = orderByFilter(collection, 'id');
}

var inc = 1;
if(tempArr.length > 0){
var lastItem = tempArr.last();
inc = lastItem.id+1;
}
you know that it does not guarantee uniqueness, and can cause problems? For such things, it is more convenient to use some implementation of UUID (CUID for example: https://github.com/ericelliott/cuid)
Well, yes - you don't need a watcher here, since you know exactly when what happened to you. And hit yourself on the hands every time you set the watcher to true with the third argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question