2015-05-07 20 views
2

我在從控制器傳遞價值,以指導

strucked我有我的控制器兩個數組

$scope.displayPeople.push(data.userName); 
$scope.displayOrg.push(data.orgname); 

我需要通過從控制這些數據的指令到指令

我的指令

<div> 

    <div class="multitext-wrap blue-border"> 

     <ul inputfocus> 

<!--<li class="tag" ng-repeat="list in selecteditemsdisplay track by $index" ng-class="{selected: $index==selectedIndex}" >--> 
       <!--<span class="tag-label">{{list}}</span><span class="tag-cross pointer" ng-click="Delete($index,selecteditemslist[$index],list,searchid)">x</span>--> 
      <!--</li>--> 

      <li class="tag" ng-repeat="list in displayItems track by $index" ng-class="{selected: $index==selectedIndex}" > 
       <span class="tag-label">{{list}}</span><span class="tag-cross pointer" ng-click="Delete($index,selecteditemslist[$index],list,searchid)">x</span> 
      </li> 

      <li class=""> 
       <input type="text" ng-model="searchModel" ng-keydown="selected=false" ng-keyup="searchItem(searchModel,searchobj)"/> 


      </li> 

     </ul> 
    </div> 

<div class="typeahead" ng-hide="!searchModel.length || selected"> 
    <div class="typeahead" ng-repeat="item in searchData | filter:searchModel | limitTo:8" ng-click="handleSelection(item,searchobj,$index,searchid)" style="cursor:pointer" ng-class="{active:isCurrent($index)}" ng-mouseenter="setCurrent($index)"> 
     <div class="bs-example"> 
    <div class="list-group list-group-item active"> 

      {{item.displayConfig[0].propertyValue}} {{item.displayConfig[1].propertyValue}} 


    </div> 
</div> 


        </div> 
       </div> 

</div> 

我用$ EMITŧ Ø由地方組織的這樣做,我得到重複發送

在控制器

$rootScope.$emit("displayEvent", {displayItems: $scope.displayPeople}); 


$rootScope.$emit("displayEvent", {displayItems: $scope.displayOrg}); 

在指令

$rootScope.$on('displayEvent', function (event, args) { 

       $scope.displayOrgs = args.displayItems; 
       console.clear(); 
       console.info($scope.displayOrgs); 
      }); 

(人和組織wher未來)

我該如何解決這個問題請預先感謝我

+1

在這種情況下,不需要$ emit,請與rilar的答案一起去 – Mwayi

回答

0

使用$ emit進行控制器和指令之間的通信不是可取的。

你需要使用 「=」 指令的範圍,允許控制器和指令等之間的雙向通信:

指令

angular.module('YourModuleName').directive('yourDirectiveName',function() { 
    return{ 
    restrict:'E', 
    scope:{ 
     displayPeople:'=', 
     displayOrg :'=', 
    }, 
link: function postLink(scope, element, attrs) { 
} 
} 
}); 


模板各自到控制器

<yourDirectiveName displayPeople="displayPeople" displayOrg ="displayOrg "></yourDirectiveName > 
3

通過聲明'範圍:假'你可以在你的指令中訪問控制者的範圍。 '假'的意思是'不要創建一個孤立的範圍,繼承控制器'。

.directive('myDirective', function() { 
    return { 
    scope: false, 
    link: function($scope){ 
     //Do stuff with $scope.displayOrgs 
     //Do stuff with $scope.displayPeople 
    } 
    }; 
}); 

此選項將創建一個隔離範圍並繼承所選變量。這是一個更乾淨的方式。

.directive('myDirective', function() { 
     return { 
     scope:{ 
      displayPeople:'=', 
      displayOrg :'=', 
     }, 
     link: function($scope){ 
      //Do stuff with $scope.displayOrgs 
      //Do stuff with $scope.displayPeople 
     } 
     }; 
}); 
相關問題