2017-07-14 7 views
0

私は何をしたいですか? 私はいくつかのhtmlの中にディレクティブを持っています。このディレクティブはボタンをレンダリングし、ボタンをクリックするとモーダルダイアログを開きます。私は、ダイアログ内で宣言されたhtmlをダイアログに表示したいと思います。このディレクティブを使用する別の場所では、モーダルダイアログで異なるHTMLメッセージが表示されることがあります。ダイアログ内にhtmlの内部htmlを表示するにはどうしたらいいですか?

ここに私が持っているコードがあります。

app.js:

var app = angular.module('plunker', ['ngResource', 'ui.bootstrap', 'ui.bootstrap.modal']); 

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'World'; 
}) 
.controller('dialogController', ['$scope', '$modalInstance', 'params', function ($scope, $modalInstance, params) { 
    $scope.info = params.info; 
    $scope.close = function() { 
    $modalInstance.dismiss('cancel'); 
    }; 
}]) 
.directive('someDirective', ['$modal', '$timeout', function ($modal, $timeout) { 
    return { 
    scope: {  
     title: '@title' 
    }, 
    restrict: 'AE', 
    replace: true, 
    templateUrl: 'someDirective.html', 
    transclude: true, 
    link: function ($scope, $element, attr, controller, transclude) { 

    //transclude(function(clone, scope) { 
    // debugger; 
    // }); 

     $scope.info = "test"; // I would like to set this to the value within the inner html of the directive. 

     $scope.openDialog = function() { 
     var modalInstance = $modal.open({ 
      templateUrl: 'dialog.html', 
      controller: 'dialogController', 
      backdrop: 'static', 
      windowClass: 'ie-app-modal-window-narrow', 
      resolve: { 
      params: function() { 
       return { 
       info: 'SomeHtml' //<-- here I want to it to the html from inside someDirective 
       }; 
      } 
      } 
     }); 
     modalInstance.result.then(function() { 
      }, 
      function() { }); 
     }; 
    } 
    }; 
}]) 


; 

dialog.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Add attachment</title> 
    <meta charset="utf-8"/> 
</head> 
<body> 
<div class="modal-header"> 
    <button type="button" class="close"><span aria-hidden="true" ng-click="close()">&times;</span></button> 
    <strong>Add Attachment</strong> 

</div> 
<div class="modal-body"> 

    <!-- Text input--> 

    <div class="alert alert-info" role="alert"> 
    {{info}} 
    </div> 
</div> 

<div class="modal-footer"> 
    <div class="form-group"> 
    <div style="float: left"> 
     <button class="btn btn-default" ng-click="close()" ng-disabled="isDisabled">Close</button> 
    </div> 
    </div> 
</div> 


</body> 
</html> 

のindex.html:

<!DOCTYPE html> 
<html ng-app="plunker"> 

    <head> 
    <meta charset="utf-8" /> 
    <title>AngularJS Plunker</title> 
    <script data-require="[email protected]" data-semver="2.2.4" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
    <link data-require="[email protected]" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
    <script data-require="[email protected]" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <script>document.write('<base href="' + document.location + '" />');</script> 
    <link rel="stylesheet" href="style.css" /> 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script> 
    <script data-require="[email protected]" data-semver="0.4.0" src="http://rawgithub.com/angular-ui/angular-ui/master/build/angular-ui.js"></script> 
    <script data-require="[email protected]" data-semver="0.12.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.min.js"></script> 
    <script data-require="[email protected]" data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular-resource.js"></script> 
    <script src="app.js"></script> 
    </head> 

    <body ng-controller="MainCtrl"> 
    <p>Hello {{name}}!</p> 
    <some-directive title="Hello"> 
     <b>Testing</b> 
    </some-directive> 
    </body> 

</html> 

someDirective.html:

<div class="panel panel-default" > 
    <div class="panel-heading"> 
    {{title}} 
    </div> 
    <div class="panel-body" ng-style="loadingStyle"> 
    <button class="btn btn-sm btn-default" ng-click="openDialog();" type="button">Open</button> 
    </div> 
</div> 

私はこれを見つけました:How to get inner html of custom directive?しかし私はそれが私の場合にどのように動作するか正確にはわかりません。私はコンパイル方法を使用していません。

Plnker

someDirectiveからダイアログに値を渡すためのおかげ

答えて

0

。 someDirectiveに まず注入$ rootScopeのようなので:

directive('someDirective', ['$modal', '$timeout', '$rootScope', function ($modal, $timeout, $rootScope) { 

は、今、私たちは新しいスコープを作成して(この場合は、$のscope.info)に値を追加し、modalScopeにそれを渡します。 angularはこれを$ scope変数としてモーダルのコントローラに渡します。

$scope.info = "test"; 

    $scope.openDialog = function() { 
    var modalScope = $rootScope.$new(); 
    modalScope.info = $scope.info; 
    var modalInstance = $modal.open({ 
     scope: modalScope, 
     templateUrl: 'dialog.html', 
     controller: 'dialogController', 
     backdrop: 'static', 
     windowClass: 'ie-app-modal-window-narrow', 
    }); 
    modalInstance.result.then(function() { 

    }, 
    function (result) { 
    }); 
    }; 

モーダルのコントローラの内部では、渡された変数にアクセスできます。

.controller('dialogController', ['$scope', '$modalInstance', function ($scope, $modalInstance) { 
    console.log($scope.info); //prints "test" 
    $scope.close = function() { 
     $modalInstance.dismiss('cancel'); 
    }; 
    }]) 

ここでは反対です。モーダルからsomeDirectiveに戻るには: いくつかの方法がありますが、私の意見では、この約束を使うのが最善の方法です。 modalInstance.resultは約束を返します。この約束は、モーダルが終了すると解決され、モーダルが解消されると拒否されます。解決済みであれ拒否であれ、値/オブジェクトはパラメータとして渡すことができます。

例は、以前のようにダイアログを開く:

$ scope.info = "テスト";

$scope.openDialog = function() { 
    var modalScope = $rootScope.$new(); 
    modalScope.info = $scope.info; 
    var modalInstance = $modal.open({ 
     scope: modalScope, 
     templateUrl: 'dialog.html', 
     controller: 'dialogController', 
     backdrop: 'static', 
     windowClass: 'ie-app-modal-window-narrow', 
    }); 
    modalInstance.result.then(function (returnStuff) { 
     //This is called when the modal is closed. 
     //returnStuff is whatever you want to return from the dialog when it's closed 
    }, 
    function (returnStuff) { 
     //This is called when the modal is dismissed. 
     //returnStuff is whatever you want to return from the dialog when it's dismissed 
    }); 
    }; 

そして、実際に近い/ダイアログをキャンセルする:(ダイアログ内から)

//pass any value you want back 
$modalInstance.close({foo:"Ayyylmao", bar:42}); 
$modalInstance.dismiss('dismissed'); 
+0

これは私が探していたものではありませんが、技術は面白いです。共有してくれてありがとう。 – costa

0

私はそれを正しく理解している場合、これはあなたが望むものである、http://plnkr.co/edit/tOe8tZ5VWfOCkocQaEKo?p=preview

$scope.info = $element.get(0).innerHTML.trim(); 
+0

正確ではありません。あなたのコードには、指示の新しい内容が戻ってきます。 – costa

0

ベースこのpostingに、私は使用している特定のangularjsバージョンで動作するものを見つけることができました。私は別のものを作ったplnkr

dialog.html:

関連する変更は、私は<div ng-bind-html="info"></div>

<!DOCTYPE html> 
<html> 
<head> 
    <title>Add attachment</title> 
    <meta charset="utf-8"/> 
</head> 
<body> 
<div class="modal-header"> 
    <button type="button" class="close"><span aria-hidden="true" ng-click="close()">&times;</span></button> 
    <strong>Add Attachment</strong> 

</div> 
<div class="modal-body"> 

    <!-- Text input--> 

    <div class="alert alert-info" role="alert"> 
    <div ng-bind-html="info"></div> 
    </div> 
</div> 

<div class="modal-footer"> 
    <div class="form-group"> 
    <div style="float: left"> 
     <button class="btn btn-default" ng-click="close()" ng-disabled="isDisabled">Close</button> 
    </div> 
    </div> 
</div> 


</body> 
</html> 

app.jsを使用する - 私はsomeDirective指令コントローラプロパティを追加し、コードはHTMLをつかみ、情報を設定しますプロパティ。

var app = angular.module('plunker', ['ngResource', 'ui.bootstrap', 'ui.bootstrap.modal']); 

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'World'; 
    }) 
    .controller('dialogController', ['$scope', '$modalInstance', 'params', function($scope, $modalInstance, params) { 
    $scope.info = params.info; 
    $scope.close = function() { 
     $modalInstance.dismiss('cancel'); 
    }; 
    }]) 
    .directive('someDirective', ['$modal', '$timeout', '$sce', function($modal, $timeout, $sce) { 
    return { 
     scope: { 
     title: '@title', 
     html: '@' 
     }, 
     restrict: 'AE', 
     replace: true, 
     templateUrl: 'someDirective.html', 
     transclude: true, 
     controller: function($scope, $element, $attrs, $transclude) { 
     $transclude(function(clone,scope){ 
      $scope.info = angular.element('<div>').append(clone).html().trim(); 
      }); 
     }, 
     link: { 
     pre: function (scope, iElement, iAttrs, controller) { 
     //debugger; 
     }, 
     post: function($scope, $element, attr, controller) { 

      //transclude(function(clone, scope) { 
      // debugger; 
      // }); 
      //debugger; 
      //$scope.info = "test"; // I would like to set this to the value within the inner html of the directive. 
      //debugger; 
      $scope.openDialog = function() { 
      var modalInstance = $modal.open({ 
       templateUrl: 'dialog.html', 
       controller: 'dialogController', 
       backdrop: 'static', 
       windowClass: 'ie-app-modal-window-narrow', 
       resolve: { 
       params: function() { 
        return { 
        info: $sce.trustAsHtml($scope.info) //<-- here I want to set it to the html from inside someDirective 
        }; 
       } 
       } 
      }); 
      modalInstance.result.then(function() {}, 
       function() {}); 
      }; 
     } 
     } 
    }; 
    }]) 


; 
関連する問題