簡體   English   中英

AngularJS-ng-init無法與$ resource和ng-repeat一起使用

[英]AngularJS - ng-init not working with $resource and ng-repeat

大家下午好,因為我還沒有找到解決問題的方法,所以我在這里= X這是交易:我正在嘗試使用ng-init調用函數來初始化ng-model,但是在此函數中im使用自定義的$ resource方法。 現在出現了問題,ng-init不等待要解決的諾言將值分配給模型,它會在其上傳遞,並且由於未解決諾言而給它分配了未定義的值。 以下是代碼:

帶輸入和ng-repeat的HTML:

<tr ng-form="itemsForm" ng-repeat="item in items">
                <td>
                  <div class="has-feedback"
                    ng-class="{
                        'has-error': itemsForm.product.$invalid && itemsForm.product.$dirty,
                      'has-success': itemsForm.product.$valid   && itemsForm.product.$touched
                    }">
                    <input type="text" typeahead-editable="false" typeahead-min-length="3" typeahead-no-results="noResultsProduct" required name="product" placeholder="Digite o codigo ou Descrição" class="form-control input-sm" id="product" typeahead-on-select="item.prod_code = product.codigo; updateFields(product)" ng-model="product" ng-init="product=item.prod_code; product=find_product(item.prod_code)" uib-typeahead="product as (product.descricao + ' - ' + product.codigo) for product in getProducts($viewValue) | orderBy:'descricao' | limitTo:15"/>
                    <p class="help-block" ng-messages="itemsForm.product.$error">
                      <span ng-message="required">Produto não informado.</span>
                      <span ng-if="noResultsProduct">Nenhum Produto encontrada.</span>
                    </p>
                  </div>
                </td>

ng-init調用的函數:

$scope.find_product = function(product) {
  if (product != null){
    console.log("Started");
    ProductService.find_by($scope.company_id,product).$promise.then(function(data) {
      console.log(data.products);
      return data.products;
    });
    console.log("Finished");
  }
}

服務功能:

angular.module(“ application”)。service('ProductService',['$ resource','$ http',function($ resource,$ http){

this.products = function(company_id, search_filter){
  var services_product = $resource('/services_product/all',
                           {},
                           { "all": { "method": "POST" }});
  return services_product.all({"company_id": company_id, "search_filter": search_filter});
};

this.find_by = function(company_id, search_filter){
  var services_product = $resource('/services_product/find_by',
                           {},
                           { "find_by": { "method": "POST" }});
  return services_product.find_by({"company_id": company_id, "search_filter": search_filter});
};

}]);

控制台中正在打印什么:

Started
document_nfe_reception_controller.js?body=1:125 Finished
document_nfe_reception_controller.js?body=1:120 Started
document_nfe_reception_controller.js?body=1:125 Finished
document_nfe_reception_controller.js?body=1:120 Started
document_nfe_reception_controller.js?body=1:125 Finished
document_nfe_reception_controller.js?body=1:120 Started
document_nfe_reception_controller.js?body=1:125 Finished
document_nfe_reception_controller.js?body=1:120 Started
document_nfe_reception_controller.js?body=1:125 Finished
document_nfe_reception_controller.js?body=1:122 Object {filial: "01  ", codigo: "1000028        ", descricao: "57 - PRENSA CABO STECK BSP- 1/2 - COD.                                          ", tipo: "MP", unidade: "PC"…}
document_nfe_reception_controller.js?body=1:122 Object {filial: "01  ", codigo: "1000028        ", descricao: "57 - PRENSA CABO STECK BSP- 1/2 - COD.                                          ", tipo: "MP", unidade: "PC"…}
document_nfe_reception_controller.js?body=1:122 Object {filial: "01  ", codigo: "1000022        ", descricao: "61 - VIGA PINUS 2 POL X 1 POL - 4M                                              ", tipo: "EM", unidade: "PC"…}
document_nfe_reception_controller.js?body=1:122 Object {filial: "01  ", codigo: "10000105       ", descricao: "61 - TABUA PINUS 3 POL X 20MM - 4000MM                                          ", tipo: "EM", unidade: "PC"…}
document_nfe_reception_controller.js?body=1:122 Object {filial: "01  ", codigo: "1000033        ", descricao: "CABO CONTROLE VEIAS NUMER.CL5 6X1,5 MM  1 KV                                    ", tipo: "MP", unidade: "MT"…}

我正在處理5個項目,如您所見,他只是在執行所有操作后才獲取數據。 感謝任何幫助=))

而不是在promise回調中返回“ data.products”,您應該將data.products分配給item.product。

$scope.find_product = function(item) {
    if (product != null){
        console.log("Started");
        ProductService.find_by($scope.company_id,item.prod_id).$promise.then(function(data) {
            console.log(data.products);
            item.product = data.products;
        });
        console.log("Finished");
    }
}

的HTML

<input type="text" typeahead-editable="false" typeahead-min-length="3" typeahead-no-results="noResultsProduct" required name="product" placeholder="Digite o codigo ou Descrição" class="form-control input-sm" id="product" typeahead-on-select="item.prod_code = product.codigo; updateFields(product)" ng-model="item.product" ng-init="find_product(item)" uib-typeahead="product as (product.descricao + ' - ' + product.codigo) for product in getProducts($viewValue) | orderBy:'descricao' | limitTo:15"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM