2017-01-11 4 views
0

最近uib-popoverで作業していますが、私は混乱しているという問題を発見しました。angle uib-popover-template入力双方向バインド

私はこのテンプレートで私は入力を持っている、最初に入力が初期値を持っている、テンプレートをpopoverしたい。

入力に基づいてコンテンツをリアルタイムで更新したいと考えています。変数をバインドするだけでは機能しませんが、オブジェクトをバインドすると機能します。私はこの問題を理解することはできません。ここで

index.htmlを

<!doctype html> 
<html ng-app="ui.bootstrap.demo"> 
    <head> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script> 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.4.0.js"></script> 
    <script src="example.js"></script> 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
    </head> 
    <body> 

<div ng-controller="PopoverDemoCtrl"> 
    <hr/> 
    <hr/> 
    <hr/> 
    <button uib-popover-template="'myPopoverTemplate.html'" popover-placement="bottom-left" type="button" class="btn btn-default">Popover With Template</button> 
    &nbsp; 
    {{dynamicPopover.title}} 
    <script type="text/ng-template" id="myPopoverTemplate.html"> 
     <div class="form-group"> 
      <input type="text" ng-model="dynamicPopover.title" class="form-control"> 
     </div> 
    </script> 

    <button uib-popover-template="'inputContent.html'" popover-placement="bottom-left" type="button" class="btn btn-default">Popover With Template</button> 
    &nbsp; 
    {{inputContent}} 
    <script type="text/ng-template" id="inputContent.html"> 
     <div class="form-group"> 
      <input type="text" ng-model="inputContent" class="form-control"> 
     </div> 
    </script> 
</div> 
    </body> 
</html> 

example.js

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']); 
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function($scope, $sce) { 
    $scope.dynamicPopover = { 
    title: 'Title' 
    }; 
    $scope.inputContent = "hello"; 
}); 

plunker例https://plnkr.co/edit/IPXb5tddEPQPPAUrjdYx?p=previewである、あなたは、tryを持つことができます。

+0

これは、変数は値によってバインドされ、オブジェクトはJavaScriptで参照によってバインドされているからだと思います。オブジェクトにバインドするだけで問題はありますか? –

+0

あなたは[プリミティブとの双方向バインディングを持つことはできません](http://stackoverflow.com/questions/38078884/two-way-binding-on-primitive-variables-in-angularjs-directive) – RamblinRose

+0

@big_water変数にバインドできない理由を理解したい –

答えて

0

あなたは2番目の大洪水で面白いことをしました。私は初めてこれをキャッチしませんでしたが、あなたがスクリプトタグであなたのポップオーバー入力を行っているためかもしれないと思います。私はカスタム・ディレクティブを使用することをお勧めします。そうすれば、角度のダイジェストサイクルで最新の状態に保たれます。

例指令:

customPopoverApp.directive('customPopover',['$compile','$templateCache',function($compile,$templateCache){ 
    return { 
    restrict:'A', 
    transclude:true, 
    template:"<span>Click on me to show the popover</span>", 
    link:function(scope,element,attr){ 
     var contentHtml = $templateCache.get("customPopover.html"); 
     contentHtml=$compile(contentHtml)(scope); 
     $(element).popover({ 
     trigger:'click', 
     html:true, 
     content:contentHtml, 
     placement:attr.popoverPlace 
     }); 
    } 
    }; 
}]); 

そして、それを使用する:

<button custom-popover="" popover-place="bottom">click on me to show the popover</button> 
{{message}} 

<script type="text/ng-template" id="customPopover.html"> 
    <div class="form-group"> 
    <input type="text" ng-model="message" class="form-control"> 
    </div> 
</script> 

ここworking plunkです。私はこれがあなたが探していることを願っています