3

我正在开发一个使用.component()withtemplate属性的 Angularjs 项目,但我不知道如何使用templateUrl. 有没有熟悉的人可以为我提供一个工作示例?谢谢。

4

2 回答 2

3

要正确使用角度组件,我建议使用 controllerAs 语法。

angular.module('myApp')
    .component('groupComponent', {
        templateUrl: 'app/components/group.html',
        controller: function GroupController(){
              this.innerProp = "inner";
        },
        controllerAs: 'GroupCtrl',
        bindings: {
            input: '<'
        }
    });

在 group.html 上,您可以通过以下方式消费:

<div>
{{GroupCtrl.input}}
{{GroupCtrl.inner}}
</div>

从父控件您可以将任何参数作为绑定传递给组件,在本例中是从父 HTML:

<group-component input="someModel">
</group-component>
于 2016-03-08T09:09:48.827 回答
1

templateUrl是模板文件的路径。

例如

app.component('myview', {
  bindings: {
    items: '='
  },
  templateUrl: 'mycollection/view.html',
  controller: function ListCtrl() {}
});

视图.html

<h1> Welcome to this view </h1>

如上例所示,目录view.html内必须有文件mycollection

于 2015-12-23T12:05:28.747 回答