cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              html(ng-app="myApp")
  body(ng-controller="InputTestController as ctrl")
    .form-wrapper
      form(name="ctrl.form")
        //- legend 输入范围测试
        .input-wrapper
          input(name="budget", ng-model="ctrl.totalBudget", num-range-validate,  min="ctrl.sum")
          i.fa.fa-exclamation.error(ng-show="ctrl.form.budget.$invalid", title="您的预算不得低于已选物品总金额{{ctrl.sum}}")
        ul
          li(ng-repeat="item in ctrl.list") 
            span.fa-stack.check(ng-click="ctrl.selectItem(item)", ng-class="{'selected': item.select, 'disable': ctrl.form.$invalid}")
              i.fa.fa-square.fa-stack-1x
              i.fa.fa-check-square.fa-stack-1x
            | {{item.name}} 价格:{{item.cost}}   
        button(type="button", ng-click="ctrl.submit()", ng-disabled="ctrl.form.$invalid") 确认
            
          
!
            
              $blueColor = #87CEEB
$whiteColor = #fff
$errorColor = #CD5555
$greyColor = #F5F5F5
*
  outline none
.form-wrapper
  width 250px
  margin 20vh auto
  text-align center
  .input-wrapper
    border 1px solid $blueColor
    height 30px
    border-radius 10px
    padding 0px 30px
    position relative
    input
      width 100%
      height 100%
      border none
      box-sizing border-box
      &.ng-invalid:not(.ng-pristine)
        color $errorColor
    .error
      position absolute
      right 10px
      cursor pointer
      top 5px
      color $errorColor
  ul
     text-align left
     list-style none
     .check
       cursor pointer
       .fa-square
         color $greyColor
       .fa-check-square
         color $blueColor
         visibility hidden
       &.selected > .fa-check-square
          visibility visible
       &.disable
          opacity 0.5
          pointer-events none
  button 
    background $blueColor
    color $whiteColor
    width 80px
    border none
    height 30px
    &[disabled]
      opacity 0.5
    
            
          
!
            
              // custome validate directive
function numRangeValidateDirective() {
   return {
        require: 'ngModel',
        scope: {
            min: '=',
            max: '='
        },
        link: (scope, elem, attr, ngModel) => {
            //For DOM -> model validation
            ngModel.$parsers.push((value) => {
                let valid = ((/^-?(0|([1-9]\d*))$/).test(`${value}`));
                const res = _.toNumber(value);
                valid = _.isUndefined(scope.min) ? valid : (valid && (res >= scope.min));
                valid = _.isUndefined(scope.max) ? valid : (valid && (res <= scope.max));
                ngModel.$setValidity('numRange', valid);
                return valid ? res : undefined; // eslint-disable-line no-undefined
            });

            //For model -> DOM validation
            // ngModel.$formatters.unshift((value) => {
            //     // ngModel.$setValidity('budget', blacklist.indexOf(value) === -1);
            //     return value;
            // });
        }
    };
}

class InputTestController {
  constructor() {
    this.list = [
      {name: '羽绒服', cost: 8000},
      {name: '雪地靴', cost: 600},
      {name: 'iPad Pro', cost: 6540},
      {name: '玩具', cost: 100},
      {name: '茶杯', cost: 80}
    ];
    this.totalBudget = 0;
    this.sum = 0;
  }
  
  _getTotalSum() {
    return _.sumBy(_.filter(this.list, ['select', true]), (item) => {return item.cost});
  }
  
  selectItem(item) {
      item.select = !item.select;
      if (item.select) {
        this.totalBudget += item.cost;
      } else {
        this.totalBudget -= item.cost;
      }
     this.sum = this._getTotalSum();
     this._setStatus();
  }
  
  _setStatus() {
    const index = _.findIndex(this.list, ['select', true]);
    if (index == -1) {
      this.totalBudget = 0;
      this.sum =0;
    }
  }
  submit() {
    alert(`您好,你的应付金额是¥${this.totalBudget}`);
  }
}
InputTestController.$inject = [];

angular.module('myApp', [])
  .directive('numRangeValidate', numRangeValidateDirective)
  .controller('InputTestController', InputTestController)
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console