Angularjs Ngmodel realizes the direct communication between instruction and input _angularjs

Source: Internet
Author: User


First look at the effect, in the drag Input[range], the following animation will change with the drag, the use of the principle is ngmodel implementation of the communication. The principle of implementation is to implement the communication between the instruction and the outside by introducing ^ngmodel in the instruction.



Here are a few points:



1, $formatters push into the function to achieve modelvalue into Valeuvalue.
2, $render method to render the value of Viewvalue into the template.
3, $setViewValue implement is to update the Viewvalue value in the template.
4, $parsers push in the way to achieve valuevalue into Modelvalue.





<! DOCTYPE html>
<html lang = "en" ng-app = "rangeApp">
<head>
  <meta charset = "UTF-8">
  <title> </ title>
</ head>
<script src = "lib / angular.min.js" type = "text / javascript"> </ script>
<script src = "lib / jquery-2.1.4.min.js" type = "text / javascript"> </ script>
<style>
  div {
    position: absolute;
  }
  .ani {
    width: 200px;
    height: 200px;
    border: 1px solid aqua;
    position: absolute;
    top: 100px;
    left: 200px;
    -webkit-transform: rotateX (120deg);
    -moz-transform: rotateX (120deg);
    -ms-transform: rotateX (120deg);
    -o-transform: rotateX (120deg);
    transform: rotateX (20deg);
  }
  .active {
    background: red;
  }
  ::-webkit-slider-runnable-track {
    border: 1px solid # a0b3d6;
    background: # f0f3f9;
  }
  ::-webkit-slider-thumb {
    outline: 1px dotted # a0b3d6;
    background-color: # 34538b;
  }
  ::-webkit-slider-runnable-track: hover {
    background-color: # cad5eb;
  }


  ::-moz-range-track {
    border: 1px solid # a0b3d6;
    height: 20px;
    background: # f0f3f9;
  }
  ::-moz-range-thumb {
    background: # 34538b;
    height: 30px;
  }

  ::-ms-fill-lower {background: orange;}
  ::-ms-fill-upper {background: green;}
  ::-ms-thumb {background: red;}
  ::-ms-ticks-before {display: block; color: black;}
  ::-ms-ticks-after {display: block; color: blue;}
  ::-ms-track {padding: 5px;}
  ::-ms-tooltip {display: none; / * Numerical prompt can only be display or visibility * /}
</ style>
<body ng-controller = "rangeCon">
<input type = "range" ng-model = "rangeModel" />

<animate-span ng-model = "rangeModel"> </ animate-span>
</ body>
<script>
  var app = angular.module ("rangeApp", [])
      .controller ("rangeCon", ["$ scope", function ($ scope) {
        $ scope.rangeModel = 30;
      }])
      .directive ("animateSpan", function () {
        return {
          restrict: 'EA',
          replace: true,
          scope: {},
          require: '^ ngModel',
          template: '<div> <span class = "ani" ng-model = "dis"> {{dis}} </ span> </ div>',
          link: function (scope, element, attr, ngModelController) {
            var rotateX = function (modelValue) {
              var modelValue = parseInt (modelValue) || 0;
              var value = "rotateX (" + modelValue * 3 + "deg)";
              return {
                dis: value
              };
            };
            var rotateY = function (modelValue) {
              var modelValue = parseInt (modelValue) || 0;
              var value = "rotateY (" + modelValue * 3 + "deg)"
              return {
                dis: value
              }
            };
            var rotateZ = function (modelValue) {
              var modelValue = parseInt (modelValue) || 0;
              var value = "rotateZ (" + modelValue * 3 + "deg)";
              return {
                dis: value
              }
            };
            //scope.dis=30;
            ngModelController. $ formatters.push (rotateZ); // The communication between modelValue and viewValeu.
            ngModelController. $ render = function () {// Render viewModel to template
              scope.dis = ngModelController. $ viewValue.dis;
              $ (element) .find ("span"). css ("-webkit-transform", scope.dis)
            };
            scope. $ watch ('dis', function () {// Update viewValue on the template
              ngModelController. $ setViewValue ({
                dis: scope.dis
              })
            })
          }
        }
      })
</ script>
</ html> 


The above is the angularngmodel to achieve instructions and input direct data data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.