我有这个指令:
app.directive('sampleDirective' , function () {
return {
controller: function ($scope) {
$scope.numbers = [
{number : '1 '},
{number : '2 '},
{number : '3'},
{number : '4'},
];
}
}
});
我在列表中将此号码称为:
<sample-directive>
<button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-dropdown dropdown-toggle">choose number<i class="icon icon-arrow-down"></i></button>
<ul class="dropdown-menu list-unstyled text-right">
<ul>
<li ng-repeat="x in numbers">
<a href = "#"> {{ x.number }} </a>
</li>
</ul>
</ul>
</sample-directive>
此时,列表可以显示数字,但我想将选择数字更改为所选值..(例如,如果我选择 1 我想将其设置为按钮的值)怎么做?
答案 0 :(得分:1)
在您的控制器中:
$scope.setSelectedNumber = function(n) {
$scope.selectedNumber = n;
}
在您的链接中:
<a href="" ng-click="setSelectedNumber(x.number)"> {{ x.number }} </a>
在你的按钮中:
{{ selectedNumber ? selectedNumber : 'choose number' }}
甚至更简单:
{{ selectedNumber || 'choose number' }}
答案 1 :(得分:0)
可能这可以工作
<a href = "#" ng-click="setNumber(x.number)"> {{ x.number }} </a>
$scope.setNumber = function(num){
$scope.btnValue = num
}
<button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-dropdown dropdown-toggle">choose number<i class="icon icon-arrow-down" ng-value="btnValue"></i></button>