【发布时间】:2017-01-11 21:41:02
【问题描述】:
我最近在做uib-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>
{{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>
{{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,你可以试试。
【问题讨论】:
-
我认为这是因为变量是由值绑定的,而对象是由javascript中的引用绑定的。只绑定到对象有什么问题?
-
@big_water 我只是想弄清楚为什么它不能绑定到变量。
-
@RamblinRose 我几乎明白了你的意思,但我仍然可以将变量绑定到输入,请看这个 plunker plnkr.co/edit/9i41WS?p=preview,为什么在这个例子中我可以绑定一个变量而不是一个对象,它有效.
标签: angularjs angular-ui-bootstrap