Is it possible to change the value of a ng-model using a filter?

1

I am making a array , but I want to change the value shown in the view according to some evaluations.

would be showing the values like this.

<div ng-repeat="valor in bonos">
 <label>{{valor.nombre |filtro: myfuncion}}</label>
</div>

Then in my function I want to return a new value for valor.nombre

vm.myfunction = funcion(valorAntiguo){
  return nuevoValor;
}
    
asked by Luis Ruiz Figueroa 16.06.2017 в 23:17
source

3 answers

1

You do not need to pass a filter, you can define a method where you evaluate and return a modified value, and call it when the original value is going to be written.

Something like this (which processes the strings and converts them to uppercase):

var app = angular.module("miApp", []);
app.controller("miCtrl", function($scope) {
  $scope.array = ["álvaro", "lucas", "sofía"];
  $scope.miFormato = function(valor) {
    return valor.toUpperCase();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="miApp" ng-controller="miCtrl">
  <div ng-repeat="val in array">
    {{ miFormato(val) }}
  </div>
</body>
    
answered by 17.06.2017 / 07:09
source
1

If you pass a function to the filter as a parameter then you must return the call to that function from the filter.

app.filter('filtro', function() {
    return function(input, myfuncion) {
        return myfuncion();
    };
});
    
answered by 17.06.2017 в 06:40
1

Hello, as you write it, you can do it in the following way,

<div ng-repeat="valor in bonos">
 <label>{{valor.nombre |filtro: myfuncion(valor)}}</label>
</div>


vm.myfunction = funcion(valor){
  valor.nombre = 'nuevoValor';
}

With that the reference is indicated and you can change the value from the function.

    
answered by 20.06.2017 в 16:30