2016-12-13 11 views
0

2つのアプリでは、すべてのアクティブなインシデントが表示され、閉じられたインシデントはすべて表示されます。 2つのアプリは、作成、変更、保存、削除の際に同様のロジックを共有します。角1 - 親と子のコントローラ間でデータを共有する - アドバイスが必要

私は、2つのアプリケーション間でCRUDロジックを共有するための最良の方法を見つけようとしています。私は多分それは親を設定するのがベストでしょう取り払わ - そのような子コントローラの設定:

Common_CRUD_file.js:

var Common_Application = .module('Common_Application ',[app, app1, app2, app3]) 
Parent_controller.controller('Parent_controller'), function ($scope, $http, $filter, $timeout) { 

//all my CRUD logic goes in here 
$scope.edit = function(data) { //edit logic goes here } 
$scope.save = function(data) { //save logic goes here } 
$scope.cancel = function(data) { //cancel logic goes here } 
$scope.delete = function(data) { //delete logic goes here } 

} 

Child_Show_closed_incidents.js:

var Common_Application = angular.module('Common_Application'); 
Child_controller.controller('Child_controller'), function ($scope, $http, $filter, $timeout) { 

//All of the app logic goes here  
$scope.get_data = function(data) { //store fetched ajax data in $scope.All_closed_incidents } 

} 

HTMLファイルのクイック抜粋:

<div ng-app="Common_Application" ng-controller="Parent_controller"> 
<div ng-controller="Child_controller"> 
<table directive_for_angular_app_here> 
    <tr> 
    <td></td> 
    <td>Description</td> 
    <td>Status</td> 
    </tr> 
    <tr ng-repeat="incident in All_closed_incidents"> 
    <td><button type="button" ng-click="edit(incident)">Edit</button></td> 
    <td>{{incident.Description}}</td> 
    <td>{{incident.Status}}</td> 
    </tr> 
</div> 
</div> 

この設定ではテーブルを読み込むことができますが、編集機能私がボタンをクリックすると、dosentが全く発射しないようです。コンソールにもエラーはありません。私はすべてのスコープを共有することを期待していたときに、私の親関数をすべて無視しているようです。誰かがこれに良いaprochを持っていますか?

+0

サービスを作成し、その中にCRUDロジックを入れ、必要な場所にこのサービスを注入してから、その機能をコントローラから呼び出す方が良いと思います。 –

答えて

2

親/子の設定を破棄します。機能とサービスに親を回し:

//all my CRUD logic goes in here 
$scope.edit = function(data) { //edit logic goes here } 
$scope.save = function(data) { //save logic goes here } 
$scope.cancel = function(data) { //cancel logic goes here } 
$scope.delete = function(data) { //delete logic goes here } 

、子コントローラにそのサービスを注入して関数を呼び出します。複雑さが軽減され、再利用性が向上します。

+0

ああありがとう、私はそれを試してみるつもりです。 –

関連する問題