在聚合物入门页面,我们看到了一个聚合物的例子:

<html>
  <head>
    <!-- 1. Shim missing platform features -->
    <script src="polymer-all/platform/platform.js"></script>
    <!-- 2. Load a component -->
    <link rel="import" href="x-foo.html">
  </head>
  <body>
    <!-- 3. Declare the component by its tag. -->
    <x-foo></x-foo>
  </body>
</html>

你会注意到<x-foo></x-foo>是由platform.js和x-foo.html定义的。

看起来这相当于AngularJS中的指令模块:

angular.module('xfoo', [])
.controller('X-Foo', ['$scope',function($scope) {
    $scope.text = 'hey hey!';
})
.directive('x-foo', function() {
    return {
        restrict: 'EA',
        replace: true,
        controller: 'X-Foo',
        templateUrl: '/views/x-foo.html',
        link: function(scope, controller) {
        }
    };
});

这两者之间有什么区别? 有哪些问题是聚合物解决了,而AngularJS还没有或不会解决的? 将来有计划把Polymer和AngularJS绑定在一起吗?


当前回答

Angular指令在概念上类似于自定义元素,但它们的实现不使用Web组件api。Angular指令是构建自定义元素的一种方式,但Polymer和Web Components规范是基于标准的方式。

polymer-element:

<polymer-element name="user-preferences" attributes="email">
  <template>
    <img src="https://secure.user-preferences.com/path/{{userID}}" />
  </template>
  <script>
    Polymer('user-preferences', {
      ready: function() {
        this.userID= md5(this.email);
      }
    });
  </script>
</polymer>

角指令:

app.directive('user-preferences', ['md5', function() {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      scope.userID= md5(attrs.email);
    },
    template: '<img src="https://secure.user-preferences.com/path/{{userID}}" />'
  };
}]);

其他回答

I think from a practical perspective, in the end the template feature of angular directives, and the web component methodology leveraged by polymer both accomplish the same task. The major differences as I can see it are that polymer leverages web APIs to include bits of HTML, a more syntactically correct, and simplistic way of achieving what Angular does programatically as it renders templates. Polymer is however as has been stated, a small framework for building declarative and interactive templates using components. It is made available for UI design purposes only, and is only supported in the most modern browsers. AngularJS is a complete MVC framework designed to make web applications declarative by use of data bindings, dependencies, and directives. They're two completely different animals. To your question, it seems to me at this point you'll get no major benefit from using polymer over angular, except having dozens of pre built components, however that would still require you to port those over to angular directives. In the future however, as the web APIs become more advanced, web components will completely remove the need to programatically define and build templates as the browser will be able to simply include them in a similar way to how it handles javascript or css files.

Angularjs directive is an approach for making custom elements. you can define new custom tags with custom attributes. Polymer can also do this but it'll do in an interesting and more simple way.Polymer actually is not a framework it's just a library.but a powerful and amazing library that you can fall in love with it (like me). Polymer let you learn the native web components technology made by w3c, that web browsers eventually implementing it.web component is the future technology, but polymer let you use that technology right now.Google Polymer is a library that provides syntactic sugar and polyfills for building elements and applications with web components.Remember that I said polymer is not a framework and it's a library.But when you're using Polymer, actually your framework is DOM.this post was about angular js ver 1 and polymer and I has been worked with both of them is my projects and I personally prefer polymer over angularjs. But Angular version 2 is completely different in compare of angularjs ver 1.directive in angular 2 has a different meaning.

这两者之间有什么区别?

对用户来说:没什么。你可以用这两种方法来构建出色的应用程序。

对于开发者来说:他们使用不同的语法,所以任何一种解决方案都有相当陡峭的学习曲线。Angular已经存在了很长时间,而且有一个庞大的社区,所以你很难找到还没有解决的问题。

To an architect: Very different. Angular is an application framework responsible for all aspects of your life. It even has directives vertically integrated in case you want component like features. Polymer on the other hand is more like pay-as-you-go. You want a modal, sure thing, you want an interactive widget, no problem, you want route handling, we can do that to. Polymer is also more portable in that Angular requires an Angular app to reuse directives. The idea with Polymer is be more moduler and will work in other apps, even Angular apps.

有哪些问题是聚合物解决了,而AngularJS还没有或不会解决的?

聚合物是一种朝向利用新的网络组件标准的方法。如果本地支持自定义元素、Shadow DOM和HTML导入等特性,那么不利用它们将是愚蠢的。目前,大多数web组件的功能还没有得到广泛的支持(目前的状态),所以聚合物充当垫片或桥梁。有点像一个填充(事实上它确实使用填充)。

将来有计划把Polymer和AngularJS绑定在一起吗?

我们一起使用Angular和Polymer已经有一年多了。这样做的部分决定是基于聚合物团队直接向我们承诺的互操作性。我们已经放弃了那个想法。我们现在正朝着只使用聚合物的方向发展。

如果再做一遍,我们可能根本就不会使用聚合物,而是等待它成熟。话虽如此,聚合物有优点(有些很好)和缺点(有些很令人沮丧),但我认为这是另一个话题的讨论。

Angular提供的MVVM(模型-视图,视图-模型)并不是Polymer想要解决的问题。当你考虑比较Angular和Polymer时,Angular指令的可组合性和可重用性(自定义标签+相关逻辑组合)是一个更明智的比较。Angular现在是,将来也仍然是一个更广泛的服务于目的的框架。

在这个视频中,来自AngularJS的两个家伙讨论了这两个框架(AngularJS 1.2和超越版本)的异同。

这些链接将带你进入正确的问答环节:

http://www.youtube.com/watch?v=W13qDdJDHp8&feature=share&t=56m34s http://www.youtube.com/watch?v=W13qDdJDHp8&feature=share&t=59m8s