我有一些指令,例如
app.directive('navContent', function() {
return {
restrict: 'E',
scope: {
content: "="
},
templateUrl: '../../' + template + '/regions/nav.html',
}
});
app.directive('headerContent', function() {
return {
restrict: 'E',
scope: {
content: "="
},
templateUrl: '../../' + template + '/regions/header.html',
}
});
所有都需要templateURL中的模板var。
我尝试了一个没有成功的功能
var siteID = angular.element('head').data('site-id');
$http.get('/api/sites/' + siteID)
.success(function(site) {
var template = site.template;
})
.error(function(data) {
console.log('Error: ' + data);
});
};
siteID在DOM中:
<head data-site-id="123456">
和site.template应该从网站模型返回模板
任何帮助,将不胜感激
$rootScope
就像@Edward Knowles的回答一样,使用共享数据是完全合法的,并且效果很好。
但是,通过将数据放到应用程序中$rootScope
,就可以将数据放到其他位置$scope
。这可能不是理想的选择(可变冲突,破坏封装,单元测试更加脆弱等)。
共享数据的另一种方法是使用Angular服务/工厂。这会将数据放入您可以注入所有控制器/指令的内容中。
app.service('SharedData', function() {
var service = {
foo: 'bar',
baz: 'bip'
};
return service;
});
app.directive('myDirective', function(SharedData) {
// do something w/SharedData
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句