2017-06-22 18 views
1

我被告知將AngularJS的component合併到我的應用程序中。在使用組件之前,我有兩個控制器,分別是patentListpatentItem。當用戶點擊patent list中的項目時,它將載入關於該具體patent item的更多詳細信息。如何訪問組件中的父母控制器數據在angularjs

現在我已將控制器包含在components中,數據無法填充patent item表。這是(我認爲)patent itempatent list的孩子,我能夠訪問父母的數據。

當我登錄所選項目時,它會顯示所有屬性和值。 console log of item selected

我的問題是,我該如何訪問父controller現在我已經使用components的數據?任何幫助將不勝感激

列表專利

<tr ng-repeat="x in $ctrl.patents"> 
    <td ng-click="$ctrl.select(x)"><a ui-sref="patents.list.item({id: x.id})">{{x.applicationNumber}}</a></td> 
    <td ng-bind="x.clientRef"></td> 
    <td ng-bind="x.currentRenewalCost">$</td> 
    <td ng-bind="x.costBandEndDate"></td> 
    <td ng-bind="x.renewalCostNextStage"></td> 
    <td ng-bind="x.renewalDueDate"></td> 
</tr> 

專利項目

<tr ng-repeat="x in $ctrl.patentItem"> 
    <td><input type="text" ng-model="x.patentApplicationNumber"></td> 
    <td><input type="text" ng-model="x.clientRef"></td> 
    <td><input type="text" ng-model="x.renewalStatus"></td> 
    <td><input type="text" ng-model="x.costBandEndDate"></td> 
    <td><input type="text" ng-model="x.renewalCostNextStage"></td> 
    <td><input type="text" ng-model="x.renewalDueDate"></td> 
</tr> 

app.js

var app = angular.module('myApp', ['ngRoute', 'ui.router']); 

app.config(function($stateProvider, $locationProvider, $urlRouterProvider, localStorageServiceProvider) { 

    app.component('patentList', { 
     scope: {}, 
     templateUrl: "templates/patents/list/list-patents.htm", 
     controller: function(loadPatentsService, loadPatentItemService) { 

      var vm = this; 

      vm.select = function(item) { 
       vm.patentItem = loadPatentItemService.select(item); 
       console.log(item) 
      } 
     } 
    }) 
}); 

app.component('patent', { 
    scope: {}, 
    templateUrl: "templates/patents/list/patent-item.htm", 
    controller: function(patentTabService, loadPatentItemService) { 

     var vm = this; 

     //LOAD OF CODE FOR A TAB PANEL 

    } 
}) 

app.factory('loadPatentItemService', function() { 

    var factory = {}; 

     factory.select = function(item) { 
      factory.storeSelect = []; 
      selectedItem = item; 
      factory.storeSelect.push(selectedItem) 
      return [selectedItem]; 
     }  

    return factory; 

}) 

app.factory('patentTabService', function() { 

    var factory = {}; 

     //CODE RELATED TO THE TAB PANEL 

    return factory; 

}); 
+0

爲什麼是一個配置塊內的'patentList'成分的定義是什麼?還請顯示路由器的狀態表。 – georgeawg

回答

0

您需要使用$broadcast$on

$scope.$broadcast ('key', {data: value}); //to set the value in parent 

$scope.$on('key', function (event, data) {}); // to get the value in child 
相關問題