2

的問題爲什麼在嵌套僞指令時綁定會丟失?

另一個指令中使用我的指令會導致丟失某些綁定,特別是在ng-repeat使用。

該指令用於我的應用程序的許多領域沒有問題。它呈現的這傳遞給它的範圍從父模板,像這樣輸入列表:

我已經使用了以下情形在整個應用程序,而不是來

<filter-header filters="filters"></filter-header> 

工作方案在迄今爲止的一個問題上。

  • $routeProvider解析過濾器列表用的WebAPI呼籲控制器
  • 控制器分配列表自身的範圍,就像這樣:$scope.filters = filters
  • 模板使用過濾頭元素並將過濾器從它的範圍的指令,如下所示:<filter-header filters="filters"></filter-header>
  • filter-header指令然後使用ng-repeat呈現過濾器而沒有問題。 $$hashKey存在於過濾器集合的每個項目中,表示綁定的存在。

失敗場景

在下列情況下,結合似乎丟失,NG-重複無法呈現任何內容。

  • $routeProvider解析過濾器列表用的WebAPI呼籲控制器
  • 控制器分配列表自身的範圍,就像這樣:$scope.filters = filters
  • 模板使用了新的元素指令,指定過濾器從它的範圍,新指令的通過屬性。
  • 指令的模板使用過濾器頭元件和過濾器傳遞從它的範圍的指示,例如:<filter-header filters="filters"></filter-header>
  • 本過濾式頭指令然後失敗TO渲染使用納克重複的過濾器。 $$hashKey不是目前在任何項目的過濾器集合。

煩人,我不能複製這Plunker ...


怪異

的指令有項目的傳遞給它的另外一個集合,columns="columns"(可以在代碼中看到下面)。列正確綁定並呈現在它自己的ng-repeat中。我看不出「列」與「過濾器」的區別,因爲兩者的使用方式幾乎完全相同。


尋找更深...

我已調試的進程一路。過濾器對象正在成功完成到最終範圍。如果我將篩選器的內容輸出到最終指令中的屏幕上,使用{{ filters }}我可以按預期看到所有篩選器。但是,在我的ng-repeat開始的下一行中,不會迭代任何過濾器。

爲了確定它不是我的列表導致的問題,我使用了一個已經在使用上述工作場景的列表,並且ng-repeat在這裏沒有渲染。

爲了確保它不是我的指令代碼導致的問題,我將它轉換爲控制器並直接路由到它(跳過嵌套指令),如上面提到的工作場景和ng-repeat現在的工作。

使用$log檢查列表,我注意到一個區別。在工作場景中,所有列表都包含列表中每個項目的$$hashKey屬性。在失敗的情況下,列表中的所有項目上缺少$$hashKey。這似乎表明綁定由於某種原因正在丟失。

有人能告訴我我的方式錯誤嗎?我在使用中看到的唯一真正區別是,在將對象傳遞到使用它的指令之前,我將該對象傳遞給中間人指令。奇怪的是,在同樣的指令中,另一個列表以非常相似的方式使用,它在ng-repeat內沒有問題地呈現,並且它的項目全部具有附加的$$hashKey屬性。


代碼

有很多涉及的代碼,所以我會盡力,並挑選出相關部分。

RouteProvider

$routeProvider.when('/Pride/Admin/AuditForms/:id', { 
    templateUrl: '/Templates/Admin/editAuditForm.html', 
    controller: 'editAuditFormController', 
    resolve: { 
     sectionFilters: function (auditFormSectionRepository) { 
      return auditFormSectionRepository.getFilters().$promise; 
     }, 
     sectionColumns: function (auditFormSectionRepository) { 
      return auditFormSectionRepository.getColumns().$promise; 
     } 
    } 
}); 

EditAuditForm控制器

prideModule.controller("editAuditFormController", function ($scope, sectionFilters, sectionColumns) { 

    $scope.sectionFilters = sectionFilters; 
    $scope.sectionColumns = sectionColumns; 
}); 

EditAuditForm模板

<audit-admin-sections audit-form="auditForm" section-filters="sectionFilters" section-columns="sectionColumns" show-deleted="false"></audit-admin-sections> 

AuditAdminSections指令

prideModule.directive('auditAdminSections', function ($log) { 
    return { 
     restrict: 'E', 
     templateUrl: 'templates/admin/auditFormSections.html', 
     scope: { 
      sectionFilters: '=', 
      sectionColumns: '=' 
     }, 
     controller: function ($scope, $route, $timeout, $location, filterLogic, auditFormSectionRepository) { 
      // do stuff 
    } 
}); 

AuditFormSections模板

<filter-header filters="sectionFilters" columns="sectionColumns"></filter-header> 

FilterHeader指令

prideModule.directive('filterHeader', function($log) { 
    return { 
     restrict: 'E', 
     templateUrl: 'templates/common/filterHeader.html', 
     scope: { 
      filters: '=', 
      columns: '=' 
     }, 
     controller: function ($scope, filterItemsRepository) { 
      $log.info("$scope.filters"); 
      $log.info($scope.filters); 
      // This will log the filters as expected, however the $$hashKey property is missing from the items 
    } 
}); 

FilterHeader模板

<!-- at this point, {{ filters }} produces the list of filters --> 
<form class="form-horizontal" ng-repeat="filter in filters"> 
    <!-- at this point, nothing renders --> 
    <label class="col-sm-2 control-label">{{ filter.friendlyName }}</label> 
</form> 

更新1

我把代碼出指令,進入一個新的控制器來模擬工作情景如上所述。現在ng-repeater按預期運行,並且$$hashKey再次出現。因此,route-> controller-> directive->指令route-> controller->指令之間的區別肯定與此有關。

值得一提的是,除了上面的代碼之外,還有其他用途的濾鏡手錶。

更新2:罪犯揭祕

我已經釘它。但它沒有任何意義。看起來好像是責怪form元素。更改爲div可解決問題。我認爲這可能是一個角度錯誤,因爲我很難理解爲什麼這可能在一種情況下工作,而不是在另一種情況下。

+0

你能提供更多的代碼 - HTML/JS的相關作品? – shershen

+0

'$$ hashkey'由'ng-repeat'添加到您的收藏中。你沒有看到這些'hashkey'這一事實,意味着你的控制器和你的'ng-repeat'之間的某個地方綁定被破壞了。沒有錯字?子/父範圍內的其他變量稱爲「過濾器」? –

+0

請發佈您的代碼 –

回答

1

到目前爲止發現了兩個修復程序,但它們比修復程序更麻煩,因爲我無法理解原始問題的原因。我仍然有興趣,如果任何人都可以指出真正的問題,但在那之前,這是最好的我:

解決方案1 ​​

多研發,調試,撓頭,重寫,我靠運氣後遇到了一個解決方案。我對它不滿意,因爲它沒有意義(除非有人能爲我詳細說明)。

這個問題似乎與form元素和使用ng-repeat屬性上,而在嵌套角directive s ... !!!這必須是一個錯誤,對吧?

溶液爲改變這一簡單:

<form class="form-horizontal" ng-repeat="filter in filters"> 
    <label class="col-sm-2 control-label">{{ filter.friendlyName }}</label> 
</form> 

這樣:

<div class="form-horizontal" ng-repeat="filter in filters"> 
    <label class="col-sm-2 control-label">{{ filter.friendlyName }}</label> 
</div> 

解決方案2

看來,$scope變量也玩起了問題的一部分。如果我爲這個特定變量(即過濾器)重命名每個指令的$scope變量名,以便它是每個指令後,form ng-repeat作品是獨一無二的。這使得它看起來像有某種的指示隔離範圍內的衝突,但是爲什麼這只是form ng-repeat一個問題令我感到困惑。因此,它仍然沒有向我解釋這種行爲的根源是什麼。

相關問題