2016-07-12 4 views
0

나는 다음과 같은 경우, 여러 개의 입력 한 형태와 내가 모든 입력 같은 방법으로 계산하지만, 컨트롤러 다양한 분야 동일한 로직을 가진 다중 입력에 하나의 컨트롤러를 사용하는 방법은 무엇입니까?

<div class="row"> 
     <input type="number" min="1" class="form-control" id="InputValorFOR" placeholder="" ng-change="findModifier()" ng-model="atrb.for"> 
     <p>{{mod.for}}</p> 
</div> 
<div class="row"> 
     <input type="number" min="1" class="form-control" id="InputValorDES" placeholder="" ng-change="findModifier()" ng-model="atrb.des"> 
     <p>{{mod.des}}</p> 
</div> 

에 값을 반환 할 필요가 있습니다

app.controller('atributosCtrl', function($scope){ 

findModifier = function() {   
    if ($scope.atrb > 1 && $scope.atrb <10) 
    { 
     if ($scope.atrb % 2 == 0) 
     { 
      $scope.mod = (($scope.atrb/2) - 5); 
     }   
    } 
};  
$scope.$watch('atrb', findModifier); }); 

내가 원하는 mod.for 또는 mod.des의 값을 변경하여 각 입력에 대한 컨트롤러를 작성하지 않아도됩니다. 하지만 나는 모델의 이름을 내가 수정하는 입력에서 전달하는 방법이 없다.

+0

왜'NG 변화 = "findModifier ()"' – batmaniac7

+0

didn를 사용하지 내가 그 일을 할 수 있다는 걸 알아, tks! Waldir의 대답은 plnkr을 통해 내가 어떻게 도움이되는지 알 수있었습니다. –

답변

0

정확하게 원하는지 모르겠지만 코드를 변경하여 작동하도록했습니다. 여기 코멘트에서 원하는 것을 말하면 제가 도와 드리겠습니다.

귀하의 HTML 수정 :

<body ng-controller="atributosCtrl"> 
    <div class="row"> 
     <input type="number" min="1" class="form-control" id="InputValorFOR" placeholder="" ng-change="findModifier('for')" ng-model="atrb.for"> 
     <p>{{mod.for}}</p> 
    </div> 
    <div class="row"> 
     <input type="number" min="1" class="form-control" id="InputValorDES" placeholder="" ng-change="findModifier('des')" ng-model="atrb.des"> 
     <p>{{mod.des}}</p> 
    </div> 
    </body> 

귀하의 JS 수정 :

app.controller('atributosCtrl', function($scope){ 
    $scope.atrb = { 
    for: null, 
    des: null 
    }; 
    $scope.mod = { 
    for: null, 
    des: null 
    }; 

    $scope.findModifier = function(type) { 
    $scope.mod[type] = null; 
    if ($scope.atrb[type] > 1 && $scope.atrb[type] <10) 
    { 
     if ($scope.atrb[type] % 2 === 0) 
     { 
      $scope.mod[type] = (($scope.atrb[type]/2) - 5); 
     }   
    } 
    } 
}); 

Plunker : https://plnkr.co/edit/aCNJQyfYXZ5vU1rc381S

0

나는 이처럼 뭔가를 기대하고 있다고 생각한다. 값이 내가 희망

사용자 정의 시계에 명중합니다 변경할 때마다 당신은

(function() { 
    "use strict"; 
    angular.module("app").directive("notifypropertychanged", notifypropertychanged); 
    function notifypropertychanged() { 
     var directive = { 
      require: "ngModel", 
      link: function ($scope, element, attrs, ngModel) { 
       $scope.$watch(attrs["notifypropertychanged"], function (newVal, oldVal) { 

        var initialValue = attrs["oldvalue"]; 

       }); 
      } 
     }; 
     return directive; 
    } 
})(); 

이 입력

<input type="number" min="1" class="form-control" notifypropertychanged="atrb.des" oldvalue=" {{::atrb.des}} " id="InputValorDES" placeholder="" ng-model="atrb.des"> 

에이 지침을 적용 아래처럼 링크 기능과 함께 사용자 지정 지침을 작성할 수 있습니다 이게 도움이

+0

이것은 정확하고 지침과 시계를 사용하는 좋은 대답 이었지만, 문제를 해결하기 위해 간단하고 직접적 이었기 때문에 Waldir의 대답을 선택했습니다. –