我有html,如下所示,在两种情况下,我都调用了相同的函数,在整个代码中有2xng-点击。两种功能都在同一个控制器中。
<div class="tagselect tagselect--frameless">
<div class="combobox__body combobox__body--open combobox__body--frameless" ng-show="focus">
<ul class="list-unstyled">
<li class="combobox__item" ng-repeat="pos in listCtrl.positions | filter:query as results"
ng-click="listCtrl.choosePosition(pos)">{{pos.name}}
</li>
</ul>
</div>
</div>
<div class="col-md-2 no-padding">
<button type="button" class="btn btn-success" ng-click="listCtrl.chosenPositions(789456)">Add</button>
</div>控制器的外观如下:
myApp.controller('ListCtrl', ['$scope', '$cookies', '$http', function ($scope, $cookies, $http) {
var listCtrl = {
candidates: [],
positions: [],
chosenPositions: [],
init: function () {
listCtrl.getCandidates();
listCtrl.getPositions();
},
getCandidates: function () {
$http.get('candidates.json').then(function (res) {
listCtrl.candidates = res.data;
});
},
getPositions: function () {
$http.get('positions.json').then(function (res) {
listCtrl.positions = res.data;
});
},
choosePosition: function (position) {
console.log(position);
}
};
listCtrl.init();
$scope.listCtrl = listCtrl;
}]);我再次检查错误拼写,并确保这不是因为功能(我创建了一个简单的控制台日志新的)。
问题是按钮点击正确调用函数,但ng重复<li ng-click="">什么都不做。我在角文档中看到ng重复创建新范围,但在我看来,只要使用引用对象listCtrlchoosePosition(),这还是可以的。
有人能告诉我我做错了什么吗?谢谢
编辑:柱塞示例:http://plnkr.co/edit/ooUQA2n1Vyj8RZtsQ1Pj?p=preview
发布于 2016-06-04 20:40:09
ng-blur正在做一些奇怪的事情,所以我建议您从ListCtrl中更改$scope.focus值,而不是使用ng-blur。
html文件
<!-- more html code -->
<!-- input without ng-blur directive -->
<input class="tagselect__input" placeholder="Position" ng-focus="focus=true" ng-model="query">
<!-- more html code -->
<li class="combobox__item" ng-repeat="pos in listCtrl.positions | filter:query as results" ng-click="listCtrl.choosePosition(pos)">{{pos.name}}
<!-- more html code -->js文件
// more code goes here.
choosePosition: function (position) {
//alert('Going to choosen position');
//$scope.query = position.name;
$scope.focus = false; // Hide div options from here.
// rest of your code.
},
// more code goes here.在这个普鲁克尔中工作
https://stackoverflow.com/questions/37633280
复制相似问题