ng-click内部的ng-click不起作用

时间:2016-06-04 17:48:15

标签: angularjs

我有html,下面看起来像一个,我在整个代码中有2x ng-click,在这两种情况下我调用相同的功能。两个功能都在同一个控制器中。

          <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;
}]);

我仔细检查missspells并确保它不是因为功能(我用一个简单的控制台日志创建一个新的)。

问题是按钮单击是否正确调用了函数,但ng-repeat <li ng-click="">没有做任何事情。我在有角度的文档中读到ng-repeat创建了新的范围,但是在我使用对象listCtrlchoosePosition()

的引用时,我认为这应该仍然很好。

有人能告诉我我做错了什么吗? 感谢

编辑:Plunker示例: http://plnkr.co/edit/ooUQA2n1Vyj8RZtsQ1Pj?p=preview

1 个答案:

答案 0 :(得分:2)

ng-blur做了一些奇怪的事情,因此我建议您更改$scope.focus的{​​{1}}值,而不是使用ListCtrl

html文件

ng-blur

js文件

<!-- 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 -->

使用此plunkr

相关问题