ionicPopup无法获得输入值

时间:2017-03-08 02:14:53

标签: angularjs ionic-framework ionicpopup

我按照$ionicPopup.show here的样本但失败了。

angular.module('main').directive('dtDiscount', function($ionicPopup) {
    return {
        require: 'ngModel',
        scope: {
            operators: '=operators',
            toggle: '=toggle',
        },
        templateUrl: 'templates/components/discount.html',
        link: function ($scope, $element, $attrs, ctrl) {

            $scope.customeDiscount = 0;
            $scope.setCustomDiscount = function(){
                $ionicPopup.show({
                    title: 'Input Your Own Discount',
                    subTitle: 'XX %off',
                    template: '<input type="number" ng-model="customeDiscount"/>', // the preset value show 0, which is expected.
                    scope: $scope,
                    buttons: [{ text: 'Cancel' },{
                        text: '<b>Confirm</b>',
                        type: 'button-positive',
                        onTap: function(e) {
                            console.log($scope.customeDiscount); // 0
                            return $scope.customeDiscount;
                        }
                    }]
                });
            }

            $scope.$watch('customeDiscount', function(value){
                console.log(value);
            });
        }
    }
})

无论我为customeDiscount输入任何值,它总是会给我0输出,因此无法更改值$scope.customeDiscount

有人可以帮我找出问题所在吗?

2 个答案:

答案 0 :(得分:3)

//old $scope.customeDiscount = 0;
$scope.data = {};

        $scope.setCustomDiscount = function(){
            $ionicPopup.show({
                title: 'Input Your Own Discount',
                subTitle: 'XX %off',
                template: '<input type="number" ng-model="data.customeDiscount"/>', // the preset value show 0, which is expected.
                scope: $scope,
                buttons: [{ text: 'Cancel' },{
                    text: '<b>Confirm</b>',
                    type: 'button-positive',
                    onTap: function(e) {
                        console.log($scope.data.customeDiscount); // 0
                        return $scope.data.customeDiscount;
                    }
                }]
            });
        }

答案 1 :(得分:0)

您是否尝试使用对象而不是2个简单的值?

$scope.data= {}

然后使用

$scope.data.customeDiscount