AngularJs - 如何使用angular.copy从一个输入字段获取值到另一个字段?

时间:2016-06-30 06:06:23

标签: javascript angularjs

我无法将值从一个字段复制到下一个字段。我在po num中使用了typeahead。如果我同时从typeahead中选择po num值,则会自动填充quantity值,并且需要从copied quantity复制quantity的值。
这是... my plunk的链接...请帮我解决这个问题。
例如: - 如果我从下拉列表中选择po num1356,则自动提取quantity值为100。我希望copied quantity值与100反映相同。

我添加了我在下面使用过的代码。请看看,让我知道我犯了哪些错误。我知道这可能是一个非常小的东西,请帮忙。提前致谢

my plunk

控制器: -

    $scope.$watch('states.purchase_order_details_ord_no', function() {
    if($scope.state && $scope.states.purchase_order_details_ord_no && $scope.states.purchase_order_details_ord_no.getTotalsshadeqty) {
      $scope.copied = angular.copy($scope.states.purchase_order_details_ord_no.getTotalsshadeqty);
    } else {
      $scope.copied = null;
    }
  });

HTML: -

    <div ng-repeat="states in states.pocomments">
       <label for="purchase_order_details_ord_no">po num</label>
          <input type="text" ng-model="states.purchase_order_details_ord_no" id="purchase_order_details_ord_no" typeahead="type as type.purchase_order_no for type in types | filter: $viewValue">
      <label for="quantity">quantity</label>
          <input type="text" id="supname" ng-model="states.purchase_order_details_ord_no" typeahead="type.getTotalsshadeqty  for type in types | filter: $viewValue">
      <label for="quantitycopy">Copied quantity</label>
          <input type="text" name="supnamecopy" id="supnamecopy" ng-model="copied">
  </div>

我的数据: -

    $scope.types = [
{
"_id": "5768e6c8bdbc5db509f0f2b2",
"created": "2016-06-21T07:03:36.504Z",
"getTotalsshadeqty": "100",
"getTotalsprice": "1000",
"getTotalsqtyprice": "100000",
"purchase_order_no": "1356",
},
{
"_id": "5767cd78f5012d790aa41a7b",
"created": "2016-06-20T11:03:20.382Z",
"getTotalsshadeqty": "12",
"getTotalsprice": "10",
"getTotalsqtyprice": "120",
"purchase_order_no": "0987",
}];

$scope.states = {
  "_id": "575691b26a5ec735128fe635",
  "pocomments": [
  {
  "_id": "575691d56a5ec735128fe636",
  "po_value_currency": "Rs",
  "value": "124",
  "rate": "24",
  "quantity": "",
  "purchase_order_details_ord_no": ""
  },
  ]

1 个答案:

答案 0 :(得分:2)

ng-repeat为创建的子项创建一个独立的范围。

如果要更改隔离范围之外的属性,则需要向模型属性的访问者添加$parent

EG。 ng-model="$parent.states.purchase_order_details_ord_no"

此外,在您的监视表达中,您错误输入了states(您检查了state)。

应该按预期工作的分叉:http://plnkr.co/edit/QAgJZToxkqvtg7pFSseO?p=preview