ng-init does not work

0

I have the following:

<tr>
    <td>{{object.valor | number:2}}</td>
    <td>
        <input type="number" ng-model="object2.valor" 
        ng-init="object2.valor=object.valor" ng-value="object.valor"/>
    </td>
</tr>

Object : {{object}}  => {valor: 15}
Object 2 : {{object2}} => {}

The funny thing that in the input text is that I see the value 15 or if I modify the input to another number if it updates the value of object2

An alternative that I have thought is in my js:

var valor = {valor:12};
$scope.object = valor;
$scope.object2 = valor;

<input type="number" ng-model="object.valor" /
<input type="number" ng-model="object2.valor" />

But if I change the value of one tb, the other one is modified, how can I make them independent?

    
asked by sirdaiz 16.03.2017 в 13:35
source

1 answer

1

Try your code in an empty angle solution and it works perfectly. Maybe there is an error in your code before reaching the ng-init that you mention.

// Code goes here
angular.module('plunker', [])
.controller('MainCtrl', function($scope){
  $scope.object = {};
  $scope.object.valor = 15;
});
<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="plunker">
  <div ng-controller="MainCtrl">
    <tr>
      <td>{{object.valor | number:2}}</td>
      <td>
        <input type="number" ng-model="object2.valor" ng-init="object2.valor=object.valor" ng-value="object.valor" />
      </td>
    </tr>
    <br /> Object : {{object}}
    <br /> Object 2 : {{object2}}
  </div>
</body>

</html>

Or here in plunker: link

    
answered by 16.03.2017 в 13:54