javascript - ng-change 不触发

标签 javascript angularjs

ng-change 无论如何都不会触发我的函数,这是 View ;

            <div ng-controller="widgets.lunchMenu as vm">
                   <label class="btn btn-transparent grey-salsa btn-circle btn-sm active">
                        <input type="radio" name="options" class="toggle" id="option1" ng-model="vm.takeCount" ng-value="0" ng-change="vm.getLunchMenus()">@L("Today")
                    </label>
                    <label class="btn btn-transparent grey-salsa btn-circle btn-sm">
                        <input type="radio" name="options" class="toggle" id="option2" ng-model="vm.takeCount" ng-value="7" ng-change="vm.getLunchMenus()">@L("Week")
                    </label>
                    <label class="btn btn-transparent grey-salsa btn-circle btn-sm">
                        <input type="radio" name="options" class="toggle" id="option3" ng-model="vm.takeCount" ng-value="30" ng-change="vm.getLunchMenus()">@L("Month")
                    </label>
            </div>

这是 Controller :

    (function () {
    appModule.controller('widgets.lunchMenu', [
    '$scope', 'abp.services.app.lunch',
    function ($scope, appService) {
        var vm = this;
        var today = new Date();
        var month = today.getMonth();

        vm.getLunchMenus = function () {
            appService.getLunchMenus({ month: month + 1, takeCount: vm.takeCount }).success(function (data) {
                vm.menus = data.items;
            });;
        };

        vm.getLunchMenus();
    }
]);
})();

有什么建议吗?感谢您的帮助。

最佳答案

为了让 ng-change 指令能够看到 vm.getLunchMenus 函数,它必须位于 $scope 上>。因此,您需要执行以下操作:

$scope.vm = this;
$scope.vm = function() { ... }

然后在你的标记中,你可以做你正在做的事情

ng-change="vm.getLunchMenus()"
<小时/>

或者你可以做一些简单的事情

$scope.getLunchMenus = function() { ... }

然后在标记中:

ng-change="getLunchmenus()"

请完全消除对 vm 变量的需求,因为 this 对指令来说没有任何意义(ng-change,等)在标记中。

关于javascript - ng-change 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31516673/

相关文章:

css - "\'“在ng风格中意味着什么?

javascript - 如何在不同的 $state 管理 ui-router 或共享 $scope

javascript - 即使服务返回 404,$http Promise 也会执行 success block

javascript - ng-click 指令不调用 Controller 函数

javascript - 我如何在 Ionic 应用程序中使用 Angular DSCacheFactory

javascript - 非换行文本的 JQuery 选择器

javascript - Ember 观察者不改变字段值

javascript - 是否可以在 $mdThemingProvider 中使用 rgb 颜色选择

javascript - jQuery 未将属性插入链接

javascript - 未设置 PHP GET 变量