简体   繁体   English

我可以将组件创建为属性吗?

[英]Can I create a component as an attribute?

I'm trying to create a component that will register itself with a parent component and provide it with information specific to that child when the parent update is called. 我正在尝试创建一个组件,该组件将向父组件注册自己,并在调用父更新时向其提供特定于该子组件的信息。

parent: 父母:

    angular
    .module('app')
    .component('parentComponent', {
        templateUrl: function ($attrs) {
            return '/Components/Component/template.cshtml';
        },
        bindings: {
            api: "=",
        },
        controller: controller
    });

function controller() {
    var vm = this;
    vm.subscribedApis = [];

    vm.$onInit = function () {
        vm.api.register = register;
        vm.api.update = update;
        vm.api.performOperation = performOperation;
    };

    function update() {
        vm.subscribedApis.forEach(function (api) {
            api.update();
        });
    }

    function register(api) {
        vm.subscribedApis.push(api);
    }

    function performOperation(viewValue){
        //do something given the childs value
    }

child: 儿童:

    angular
    .module('app')
    .component('childComponent', {
        require: ['^parentComponent', 'ngModel'],
        bindings: {
            parentApi: "<",
        },
        link: function (scope, element, attrs, controller) {
            controller.getViewValue = function () {
                return ngModel.$viewValue;
            }
        },
        controller: childController
    });

function childController() {
    var vm = this;
    vm.$onInit = function () {
        vm.api = {};
        vm.api.update = update;
        vm.parentApi.register(vm.api);
        update();
    };
    function update() {
        var tag = filterTagApi.performOperation(vm.getViewValue());
    }

my problem is that I wish to use the child tag like this 我的问题是我希望像这样使用子标签

<input type="text" id="title" class="form-control input-sm"
       ng-model="search.parameters.title" autofocus
       child-component parent-api="parentApi" />

<select class="form-control input-sm" ng-model="search.parameters.typeId"
        ng-options="lookup.id as lookup.lookupValue for lookup in lookups.typesOfSomething"
        child-component parent-api="parentApi">
    <option value="" selected>All</option>
</select>

Is this possible to do with a generic child component that I can attach to different elements containing an ng-model, or do I need to find a different approach? 这可能与我可以附加到包含ng-model的不同元素的通用子组件有关吗,还是我需要找到其他方法?

Found my answer. 找到了我的答案。 Components restrict to elements and do not allow for usage as an attribute. 组件仅限于元素,不允许将其用作属性。 I'll have to make it a directive if I want it available as an attribute. 如果我希望将其作为属性使用,则必须使其成为指令。

Source: https://docs.angularjs.org/guide/component#creating-and-configuring-a-component 来源: https//docs.angularjs.org/guide/component#creating-and-configuring-a-component

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM