首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ng-点击内部ng-重复不起作用

ng-点击内部ng-重复不起作用
EN

Stack Overflow用户
提问于 2016-06-04 17:48:14
回答 1查看 717关注 0票数 0

我有html,如下所示,在两种情况下,我都调用了相同的函数,在整个代码中有2xng-点击。两种功能都在同一个控制器中。

代码语言:javascript
复制
          <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>

控制器的外观如下:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-04 20:40:09

ng-blur正在做一些奇怪的事情,所以我建议您从ListCtrl中更改$scope.focus值,而不是使用ng-blur

html文件

代码语言:javascript
复制
<!-- 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文件

代码语言:javascript
复制
// 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.

在这个普鲁克尔中工作

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37633280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档