2017-03-08 93 views
0

我按照樣本$ionicPopup.show here但失敗。ionicPopup無法獲得輸入值

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不能改變。

有人可以幫我找出問題所在嗎?

回答

1
//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; 
        } 
       }] 
      }); 
     } 
+0

THX,它幫助。該功能必須由數據包裝。 – user3711105

0

您是否試過使用對象而不是2個簡單的值?

$scope.data= {} 

然後用

$scope.data.customeDiscount