2017-11-16 1 views
0

Ich versuche, eine Komponente zu erstellen, die sich bei einer übergeordneten Komponente registriert und ihr spezifische Informationen für das untergeordnete Element bereitstellt, wenn das übergeordnete Update aufgerufen wird.Kann ich eine Komponente als Attribut erstellen?

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 
    } 

Kind:

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()); 
    } 

mein Problem ist, dass es das Kind Tag wie diese

<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> 

Ist das möglich nutzen möchte mit einem allgemeinen zu tun Kind-Komponente, die ich an verschiedene Elemente anhängen kann, die ein ng-Modell enthalten, oder muss ich einen anderen Ansatz finden?

Antwort

Verwandte Themen