Angularjs - 将参数传递给指令

SKY*_*ine 62 arguments controller directive angularjs

我想知道是否有办法将参数传递给指令?

我想要做的是从控制器附加一个指令,如下所示:

$scope.title = "title";
$scope.title2 = "title2";

angular.element(document.getElementById('wrapper')).append('<directive_name></directive_name>');
Run Code Online (Sandbox Code Playgroud)

是否可以同时传递参数,以便我的指令模板的内容可以链接到一个或另一个范围?

这是指令:

app.directive("directive_name", function(){
    return {
        restrict:'E',
        transclude:true,
        template:'<div class="title"><h2>{{title}}</h3></div>',
        replace:true
    };
})
Run Code Online (Sandbox Code Playgroud)

如果我想使用相同的指令但使用$ scope.title2会怎么样?

PzY*_*Yon 139

您可以像使用内置Angular指令一样将参数传递给自定义指令 - 通过在directive-element上指定一个属性:

angular.element(document.getElementById('wrapper'))
       .append('<directive-name title="title2"></directive-name>');
Run Code Online (Sandbox Code Playgroud)

您需要做的是scope在指令的工厂函数中定义(包括参数/参数).在下面的示例中,该指令采用title参数.然后,您可以使用它,例如在template使用常规Angular-way中:{{title}}

app.directive('directiveName', function(){
   return {
      restrict:'E',
      scope: {
         title: '@'
      },
      template:'<div class="title"><h2>{{title}}</h2></div>'
   };
});
Run Code Online (Sandbox Code Playgroud)

根据您想要绑定的方式/内容,您有不同的选择:

  • = 是双向约束
  • @ 只需读取值(单向绑定)
  • & 用于绑定函数

在某些情况下,您可能需要使用与"内部"名称不同的"外部"名称.对于外部,我的意思是指令元素上的属性名称,而内部是指在指令范围内使用的变量的名称.

例如,如果我们查看上面的指令,您可能不希望为标题指定另一个附加属性,即使您在内部想要使用title-property.相反,您想要使用您的指令,如下所示:

<directive-name="title2"></directive-name>
Run Code Online (Sandbox Code Playgroud)

这可以通过在范围定义中指定上述选项背后的名称来实现:

scope: {
    title: '@directiveName'
}
Run Code Online (Sandbox Code Playgroud)

还请注意以下事项:

  • HTML5规范说自定义属性(这基本上是Angular应用程序中的所有内容)都应该加上前缀data-.Angular通过data-从任何属性中剥离-prefix来支持此功能.所以在上面的例子中你可以在element(data-title="title2")上指定属性,内部的一切都是一样的.
  • 元素上的属性总是以<div data-my-attribute="..." />代码的形式出现(例如,范围对象上的属性),它们的形式为myAttribute.在我意识到这一点之前,我失去了很多时间.
  • 对于在不同Angular组件(控制器,指令)之间交换/共享数据的另一种方法,您可能希望查看服务或指令控制器.
  • 您可以在Angular主页(指令)上找到更多信息


SKY*_*ine 7

以下是我解决问题的方法:

指示

app.directive("directive_name", function(){
    return {
        restrict: 'E',
        transclude: true,
        template: function(elem, attr){
           return '<div><h2>{{'+attr.scope+'}}</h2></div>';
        },
        replace: true
    };
})
Run Code Online (Sandbox Code Playgroud)

调节器

$scope.building = function(data){
    var chart = angular.element(document.createElement('directive_name'));
    chart.attr('scope', data);
    $compile(chart)($scope);
    angular.element(document.getElementById('wrapper')).append(chart);
  }
Run Code Online (Sandbox Code Playgroud)

我现在可以通过相同的指令使用不同的范围并动态地附加它们.

  • 你能解释一下吗? (2认同)

SDK*_*SDK 6

您可以尝试如下:

app.directive("directive_name", function(){
return {
    restrict:'E',
    transclude:true,
    template:'<div class="title"><h2>{{title}}</h3></div>',
    scope:{
      accept:"="
    },
    replace:true
  };
})
Run Code Online (Sandbox Code Playgroud)

它在'accept'属性的值和父范围之间建立双向绑定.

您还可以使用属性设置双向数据绑定:'='

例如,如果您希望键和值都绑定到本地范围,您将执行以下操作:

  scope:{
    key:'=',
    value:'='
  },
Run Code Online (Sandbox Code Playgroud)

有关详细信息, 请访问https://docs.angularjs.org/guide/directive

所以,如果你想将一个参数从控制器传递给指令,那么请参考下面的小提琴

http://jsfiddle.net/jaimem/y85Ft/7/

希望能帮助到你..


SAN*_*.H. 5

控制器代码

myApp.controller('mainController', ['$scope', '$log', function($scope, $log) {
    $scope.person = {
        name:"sangeetha PH",
       address:"first Block"
    }
}]);
Run Code Online (Sandbox Code Playgroud)

指令代码

myApp.directive('searchResult',function(){
   return{
       restrict:'AECM',
       templateUrl:'directives/search.html',
       replace: true,
       scope:{
           personName:"@",
           personAddress:"@"
       }
   } 
});
Run Code Online (Sandbox Code Playgroud)

用法

文件:directives/search.html
内容:

<h1>{{personName}} </h1>
<h2>{{personAddress}}</h2>
Run Code Online (Sandbox Code Playgroud)

我们使用指令的文件

<search-result person-name="{{person.name}}" person-address="{{person.address}}"></search-result>
Run Code Online (Sandbox Code Playgroud)