[英]How can I pass a value service to a component?
I'm trying to turn a directive into a component. 我正在尝试将指令转换为组件。
I have a value service ('path') defined on the app, which returns a path that was used to create the template url. 我在应用程序上定义了一个价值服务(“路径”),该服务返回用于创建模板网址的路径。 With the directive it could be injected into the function and then used to create the template url like this:
使用指令可以将其注入到函数中,然后用于创建模板网址,如下所示:
.directive('myCustomer', [ 'path', function(path) {
return {
templateUrl: path + '/customer.html'
};
}]);
How can I pass 'path' to a component, where I have an object instead of the function? 如何将“路径”传递给组件,而在该组件中我有一个对象而不是函数?
Use this. 用这个。
template: '<div ng-include="$ctrl.templateUrl">'
store the value in templateUrl using component binding. 使用组件绑定将值存储在templateUrl中。
app.component('myCustomer', {
template: `<div ng-include="$ctrl.templateUrl"></div>`,
controller: ['path', function(path) {
this.templateUrl = path + '/customer.html'
}]
});
Note: As components use isolate scope, appropriate bindings need to be added to both the component and the HTML where it is instantiated. 注意:由于组件使用隔离范围,因此需要将适当的绑定添加到组件和实例化HTML的位置。
For more information, see AngularJS Developer Guide - Component-based application architecture . 有关更多信息,请参阅《 AngularJS开发人员指南-基于组件的应用程序体系结构》 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.