我正在寻找 Angular 1 组件和 CMS 驱动方法的最佳实践。
我计划构建多个标签模板,我希望这个项目是组件驱动的、高度可重用的并且由 CMS 内容驱动。
我计划将 JSON 用作组件树,并使用 $compile 服务逐步编译树,如下所示:
angular.module('app.compile', [], function($compileProvider) {
$compileProvider.directive('compile', function($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
});
});
http://plnkr.co/edit/MwUjE9l6U5wMkE89kwqY?p=preview
- 我想知道以前是否有人已经尝试过并可以分享他的反馈?
- 这听起来是个不错的解决方案吗?这是可用的最佳做法吗?
- 这种使用
$compile
服务的方法是否会降低性能?
最佳答案
我更喜欢使用 Angular 组件。我使用 $compile 只是为了动态使用我的组件。仅使用指令来更改 DOM。如果您的控件有模板,请改用组件。
检查样本 angular components with typescript
谢谢!
关于javascript - 组件和 cms 驱动应用程序的 AngularJS 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36245698/