2015-04-28 8 views
1

Как получить значение index от директивы к контроллеру. Я пробовал, но я получал индекс в директиве, но мне нужно передать его контроллеру, когда я передавал его контроллеру, я получаю неопределенный.Как получить индекс от директивы к контроллеру

, пожалуйста, помогите мне, заранее.

директива HTML

<li class="tag" ng-repeat="lists in list" ng-if="lists.userName"> 
    <span class="tag-label">{{lists.userName}}</span><span class="tag-cross pointer" ng-click="delete($index,'people', list);">x</span> 
</li> 

directive.js

.directive('search', function ($timeout) { 
    return { 
     restrict: 'AEC', 
     scope: { 
      items: '=', 
      listitem: '=', 
      prompt: '@', 
      title: '@', 
      subtitle: '@', 
      model: '=', 
      list: '=', 
      onSelectupdate: '&', 
      onDelete: '&', 
      index: '=' 


     }, 
     link: function (scope, elem, attrs, index) { 
      scope.handleSelection = function (selectedItem) { 
       scope.model = selectedItem; 
       console.warn(scope.model); 
       console.warn(scope.items); 
       console.warn(scope.model); 
//    scope.searchModel = selectedItem.displayConfig[0].propertyValue; 
       console.warn(scope.items); 
       console.warn(scope.model); 
       scope.current = 0; 
       scope.selected = true; 
       $timeout(function() { 
        scope.onSelectupdate(); 

       }, 200); 
      }; 
      scope.delete = function (index) { 
       alert('index'+index); 
       var index= index; 
       $timeout(function() { 
        scope.onDelete(); 

       }, 200); 
      }; 

      scope.$watch("items", function (newData) { 
       console.log("Items: ", newData); 
      }); 
      scope.current = 0; 
      scope.selected = true; 
      scope.isCurrent = function (index) { 
       return scope.current == index; 
      }; 
      scope.setCurrent = function (index) { 
       scope.current = index; 
      }; 
     }, 
     templateUrl: TAPPLENT_CONFIG.HTML_ENDPOINT[0] + 'home/genericsearch.html' 
    } 
}) 

controller.js

$scope.removeRecipient = function (index, type, data) { 
     console.warn(type); 
     alert("index"+index) 
     if (type == 'orgs') { 
      $scope.recipientsOrg.splice(index, 1); 
      $scope.recipientsOrgIdArr.splice(index, 1); 
     } else if (type == 'people') { 
      $scope.recipientsPeople.splice(index, 1); 
      $scope.recipientsPeopleIdArr.splice(index, 1); 
     } 
    } 

HTML

<search items="orgList" list="selectedOrgs" index="$index" listitem="list.displayConfig[0].propertyValue" model="_id" on-selectupdate="updateOrgs(_id,data.selectedOrg)" ng-keyup="getOrgs(data.selectedOrg, $event)" on-delete="removeOrg($index);" /> 
+2

запутанным, где вы используете '' это в 'нг-repeat'? если нет, то он не будет иметь '$ index', поэтому атрибут будет возвращен undefined, как будет' removeOrg ($ index) '. Демо поможет – charlietfl

+0

вам может понадобиться использовать '$ parent.delete ($ index)' & no need of extra params ''people', list' –

ответ

1

Пожалуйста, проверьте вам ng-repeat, вы, кажется, перевернутый синтаксис, он должен быть list in lists и не lists in list:

<li class="tag" ng-repeat="list in lists" ng-if="lists.userName"> 
Смежные вопросы