Javascript 将变量值传递给angularjs指令

Javascript 将变量值传递给angularjs指令,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我是angularjs的新手,我正在学习指令,我试图将值传递给指令,但这一切都不起作用 html 您似乎误解了$scope和/或如何将值传递给函数 控制器的$scope具有属性objectType。您不需要将其作为参数添加到控制器内的函数中 相反,您只需通过$scope.objectType访问它 例如 这是您使用链接并将其传递给控制器的地方 Html: 额外提示:要记住linkfunction中的变量,只需考虑sea。范围、元素、属性要做到这一点,您可以在指令控制器中使用链接功能 Html 之

我是angularjs的新手,我正在学习指令,我试图将值传递给指令,但这一切都不起作用

html


您似乎误解了$scope和/或如何将值传递给函数

控制器的$scope具有属性objectType。您不需要将其作为参数添加到控制器内的函数中

相反,您只需通过$scope.objectType访问它

例如


这是您使用链接并将其传递给控制器的地方

Html:


额外提示:要记住linkfunction中的变量,只需考虑sea。范围、元素、属性要做到这一点,您可以在指令控制器中使用链接功能

Html


之后,您可以通过scope访问控制器中的myParam

到底什么不起作用?感谢您的回复,但问题是我需要将tei_org传递给OrganizationSearchEventHandler如何使用$scope执行此操作我无法做到。请帮助meI思考属性应该是html中的对象类型,而不是objectType。这加上Rouby所说的你不需要$observe,这就是范围定义的作用和目的。而且我认为这个答案并没有解决问题的根源,而是提供了一种不同的方法。对不起,很明显,刚才读到的我正试图将值传递给指令,但它不起作用,也没有计算出来。在$observe上,如果属性值更改,则需要它。作用域定义“=”是一个双向绑定并跟踪更改。这就是原始代码所使用的。指令中的作用域设置已经涵盖了这一点。@harrini看看我的答案,我认为这个答案不是你的解决方案。
<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'

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

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

          },
          function(err){

          });
      }
<my-fancy-directive my-attribute="something"></my-fancy-directive>
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;
      })
   }
});
<genericsearch objectType="tei_org" organisationSearch="organisationSearchEvent"></genericsearch>
    link:function(scope,elem,attrs) {    
       scope.myParam = attrs.organisationSearch; 
      }