简体   繁体   中英

ng-change does not trigger

ng-change does not trigger my function anyhow, here is the 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>

here is the 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();
    }
]);
})();

any suggestion ? thanks for helping.

In order for the ng-change directive to be able to see the vm.getLunchMenus function, it has to be on the $scope . So you'd need to do something along the lines of:

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

Then in your markup, you could do what you're doing with

ng-change="vm.getLunchMenus()"

Or you could just do something as simple as

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

Then in the markup:

ng-change="getLunchmenus()"

Do completely remove the need for the vm variable, since this doesn't really mean anything to the directives ( ng-change , etc.) in the markup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM