Angular 1.5 组件:传递函数

Angular 1.5 Component: passing a function

是否可以将函数传递给组件并在传递参数的组件内部调用此函数?

示例:

post列表

<post-list posts="blog.posts"
           loading="blog.loadingPosts"
           get-post-url="blog.getPostUrl" 
           is-user-authenticate="blog.user">
</post-list>

getPostUrl是一个函数(在容器控制器内):

const getPostUrl = (postId) => {
    const protocol = $location.protocol();
    const host = $location.host();
    const port = $location.port();

    return protocol + "://" + host + "" + (port !== 80 ? ":" + port : "") + "/blog/post/" + postId;
};

post 的列表:组件

const PostList = {
  "bindings": {
    "posts": "<",
    "loading": "<",
    "getPostUrl": "&", //Function getPostUrl
    "isUserAuthenticate": "<"
  },
  "template": `<div>
                <div class="col-md-9 text-center" data-ng-if="$ctrl.loading">
                  <i class="fa fa-spinner fa-spin fa-2x"></i>
                </div>

                <div class="col-md-9 posts" data-ng-if="!$ctrl.loading">
                  <div data-ng-repeat="post in $ctrl.posts">
                    <post creation-date="{{post.creationDate}}"
                          content="{{post.content}}"
                          post-url="{{$ctrl.getPostUrl(post.creationDate)}}"
                          is-user-authenticate="$ctrl.user">
                    </post>
                  </div>
                </div>
              </div>`,
   "transclude": false
};

 angular
  .module("blog")
  .component("postList", PostList);

这一行:

post-url="{{$ctrl.getPostUrl(post.creationDate)}}" 我想调用传递参数的函数,这个函数返回一个 string.

post组件(不是PostList)中postUrl是一个字符串属性@.

但是...不起作用!

angular.js:13550 Error: [$interpolate:interr] Can't interpolate: {{$ctrl.getPostUrl(post.creationDate)}} TypeError: Cannot use 'in' operator to search for 'blog' in 1459329888892 Error Link

可以吗?又如何?

非常感谢!

如果您想从组件内部调用该函数并为其赋予 return 一个值,那么您需要双向绑定:

"bindings": {
  "posts": "<",
  "loading": "<",
  "getPostUrl": "=", // <-- two-way binding
  "isUserAuthenticate": "<"
},

但是,这可能不是一个好主意。考虑 将数据传递给组件 而不是让组件从外部请求数据。这将使隔离组件更好。

您可以将函数传递给组件,但您必须将函数参数定义为对象,并将正确的参数名称作为其键。 示例:

<post-list posts="blog.posts"
           loading="blog.loadingPosts"
           get-post-url="blog.getPostUrl(postId)" 
           is-user-authenticate="blog.user">
</post-list>

const PostList = {
 "bindings": {
  "posts": "<",
  "loading": "<",
  "getPostUrl": "&", //Function getPostUrl
  "isUserAuthenticate": "<"
 },
 "template": `<post creation-date="{{post.creationDate}}"
                      content="{{post.content}}"
                      post-url="{{$ctrl.getPostUrl({postId:post.creationDate})}}">
                </post>

要return绑定函数的值,您必须将其作为对象文字传递。
self.selected({id: '42',名字:'Douglas',姓氏:'Adams'});

angular.module('webapp').component('myComponent', {
    templateUrl: 'myComponent.html',

    bindings: {
        selected: '&'
    },
    controller: function () {
        var self = this;

        self.someEvent= function(){
            self.selected({id: '42', firstname: 'Douglas', lastname: 'Adams'});
        };
    }
});

之后您可以通过它的属性访问对象文字值。
身份证、名字、姓氏。
您还可以将其他参数传递给该函数。 (我的变量)

<div>
    <span ng-init="myVariable='Universe'">
    <my-component selected="myFunction(id, firstname, lastname, myVariable)"></my-component>
</div>


$scope.myFunction = function(id, firstname, lastname, myVariable){
    console.log(id, firstname, lastname, myVariable);    
}

根据 Todd Motto 的标准,不建议使用“=”,而是尝试使用“&”,它将方法从父组件传递到子组件,并且在子组件中调用该方法将在父组件中触发。举个例子吧。

父组件模板(HTML):

<child take-me-to-school="$ctrl.searchBikeKeyAndStart>

子组件的控制器:

public someFunction = () => {
  this.takeMeToSchool();
}

一旦从子组件的控制器调用该函数,就会触发映射到父组件的函数。

父组件的控制器(HTML):

public searchBikeKeyAndStart = () => {
  .....
}

当你想将参数传递给同一个函数时

父组件模板(HTML):

<child take-me-to-school="$ctrl.searchBikeKeyAndStart(**key**)>

子组件的控制器:

public someFunction = () => {
  this.takeMeToSchool({key: parameterValue});
}

一旦从子组件的控制器调用该函数,就会触发映射到父组件的函数。

父组件的控制器(HTML):

public searchBikeKeyAndStart = (**key**) => {
  console.log(**key**) //will print the param passed
}