2015-04-23 83 views
1

我是新來的angularjs,我正在學習指令,我試圖將值傳遞給指令,但是這個東西沒有工作。將一個變量的值傳遞給angularjs指令

HTML

<genericsearch objectType="tei_org" organisationSearch="organisationSearchEvent"></genericsearch> 

指令

directive('genericsearch', [function() { 
return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
     objectType : '=', 

     }, 
    controller: ['$scope','$element','$rootScope','SearchOrg','MapOrgs','$routeParams','DelOrgs','GetTemplateGroups','FetchOrgs', function($scope,$element,$rootScope,SearchOrg,MapOrgs,$routeParams,DelOrgs,GetTemplateGroups,FetchOrgs){ 
     $scope.getOrgs = function(objectType, event) { 
      if(event.keyCode != 13){ 
      //$scope.Participants(data); 
      $scope.organisationSearchEvent(objectType); 
      } 
     } 
     $scope.organisationSearchEvent = function(filter,objectType){ 
      SearchOrg().fetch({'filter':filter, 'searchType':objectType}).$promise.then(

      function(value){ 
      $scope.orgList = value.data; 

      }, 
      function(err){ 

      }); 
     } 
    }], 
    templateUrl : TAPPLENT_CONFIG.HTML_ENDPOINT[0]+'home/search.html' 

} 
}]) 
+0

到底什麼是 「不工作」? – gefei

+0

感謝您的回覆,但事情是我需要傳遞tei_org到organizationsearchEvent eventhandler如何通過使用$範圍做到這一點,我無法做到這一點。請幫我 – harrini

+0

我覺得屬性應該是html中的對象類型而不是objectType。這加上Rouby說的 – sirrocco

回答

0

你似乎曲解$範圍和/或如何將值傳遞到函數。

控制器的$範圍有一個屬性objectType。您不需要將此作爲參數添加到控制器內的功能。

您只需通過$scope.objectType即可訪問。

E.g.

$scope.organisationSearchEvent = function(filter){ 
      SearchOrg().fetch({'filter':filter, 'searchType':$scope.objectType}).$promise.then(

      function(value){ 
      $scope.orgList = value.data; 

      }, 
      function(err){ 

      }); 
     } 
-1

這是您使用鏈接並將其傳遞到您的控制器的地方。

HTML:

<my-fancy-directive my-attribute="something"></my-fancy-directive> 

的Javascript:

app.directive('myFancyDirective', function() { 
    restrict:'E', 
    replace:true, 
    controller:function($scope){ 
     $scope.theAttribute = ""; 
    }, 

    link:function(scope,elem,attr) { 
     attr.$observe('myAttribute',function(newValue,oldValue){ 
      if(newValue != oldValue) 
      scope.theAttribute = newValue; 
     }) 
    } 
}); 

特別提示:記住變量在linkfunction,只是覺得 「海」。範圍,元素,屬性

+0

你不需要$觀察,這就是範圍定義的作用。 – Rouby

+0

此外,我認爲這個答案沒有解決問題的根源,而只是提供了一種不同的方法。 – Rouby

+0

對不起,顯然只是閱讀「我正試圖將價值傳遞給指令,但事情沒有起作用」,並計算出來。在$觀察中,如果屬性值更改,您將需要它。 –

0

爲此,您可以在您的指令控制器中使用鏈接功能。

的Html

<genericsearch objectType="tei_org" organisationSearch="organisationSearchEvent"></genericsearch> 

指令

link:function(scope,elem,attrs) {  
     scope.myParam = attrs.organisationSearch; 
     } 

之後,你可以通過你的範圍訪問myParam在控制器

+0

這已經在指令的範圍設置上被角度覆蓋了。 – Rouby

+0

感謝您的回覆,但我沒有定義。 – harrini

+0

@harrini看看我的答案,這個答案不是我想的你的解決方案。 – Rouby

相關問題