AngularJS组件和cms驱动应用程序的最佳实践

Bna*_*Zil 5 javascript performance components compilation angularjs

我正在寻找角度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);
            }
        );
    };
  });
});
Run Code Online (Sandbox Code Playgroud)

http://plnkr.co/edit/MwUjE9l6U5wMkE89kwqY?p=preview

  1. 我想知道是否有人之前已经尝试过,可以分享他的反馈意见吗?
  2. 这听起来不错吗?这是最好的做法吗?
  3. 这种使用$compile服务的方法可能对性能有害吗?

小智 1

我更喜欢使用角度分量。我使用 $compile 只是为了动态使用我的组件。使用指令仅用于更改 DOM。如果您的控件有模板,请改用组件。

使用打字稿检查示例角度分量

谢了!