AngularStrap. 2-way binding in popover

喜你入骨 提交于 2019-12-11 04:18:42

问题


Good day,

I can successfully bind content into the popover body via template, but changes made inside this area are not bound back to the parent scope. Here is a quick example. Plunker

I would be grateful for any help

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <title>2-way binding popover</title>
</head>
<body>
<div data-ng-app="myApp" data-ng-controller="defaultCtrl" style="margin: 100px 100px">
    <button type="button" class="btn btn-lg btn-danger" data-unique="1" data-auto-close="1" data-placement="bottom" data-content-template="popover-tmpl.html" data-title="Title Two" data-html="true" bs-popover>
        Change name
    </button>
    {{ name }}
    <script type="text/ng-template" id="popover-tmpl.html">
        {{ name }}
        <input type="text" data-ng-model="name">
    </script>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.5/angular-strap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.5/angular-strap.tpl.min.js"></script>

<script type="text/javascript">
    var app = angular.module("myApp", ['mgcrea.ngStrap']);
    app.controller("defaultCtrl", ["$scope", function($scope) {
        $scope.name = 'Roman';
    }]);
</script>
</body>
</html>

回答1:


You're trying to bind a primitive. In angular that causes problems because of the childscope. Binding to an object however will work. Change your scope declaration to:

$scope.user = {
    name: 'Roman'
}

And your bindings to user.name. Here's an updated Plunker: http://plnkr.co/edit/ZEvXi9wFF8FVh0WFxn3l?p=preview



来源:https://stackoverflow.com/questions/27925166/angularstrap-2-way-binding-in-popover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!