2016-12-06 43 views
0

所以这是一个简单的例子,我刚刚为这个问题写了一篇文章,我很好奇,如果我能够使用指令并将对象传递给指令的属性,全部在模板中,not templateUrl。我认为它会这样工作:我可以在模板函数中使用指令吗?

angular.module('myModule') 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }); 

回答

0

是的,你可以!定义顺序并不重要。

app.directive("directive1", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      u: '@' 
     }, 
     template : "<h4>{{u}}</h4><directive2 user='{{u}}'></directive2>" 
    }; 
}); 
app.directive("directive2", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      user: '@' 
     }, 
     template : "<h1>{{user}}</h1>" 
    }; 
}); 

看到它在行动在这个jsfiddle

+0

感谢您的回复!你能否展开这个例子来传递一个属性?最好是对象 – Jesse

+0

当然!现在看。 – driconmax

+0

我使用了一个字符串,以方便测试并查看结果。如果你想用一个对象用'='改变'@'并改变你打印的内容(例如'u.name') – driconmax

0

是的,这很容易通过scope属性来传递你的状态。这是一个plunkr,演示了这个概念。

app = angular.module('app',[]) 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }).directive('avatar', function() { 
    return { 
     scope: { 
     user: '=', 
     }, 
     template: '<span>{{user.name}}</span>', 
    }; 
    }) 
相关问题