zoukankan      html  css  js  c++  java
  • 封装第三方jquery插件

    需要自己编写 directives 的情况通常是当你使用了第三方的 jQuery 插件。因为插件在 AngularJS 之外对表单值进行更改,并不能即时反应到 Model 中。例如我们用得比较多的 jQueryUI datepicker 插件,当你选中一个日期后,插件会将日期字符串填到 input 输入框中。View 改变了,却并没有更新 Model,因为$('.datepicker').datepicker(); 这段代码不属于 AngularJS 的管理范围。我们需要编写一个directive 来让 DOM 的改变即时更新到 Model 里。

    var directives = angular.module('directives', []);
     
    directives.directive('datepicker', function() {
       return function(scope, element, attrs) {
           element.datepicker({
               inline: true,
               dateFormat: 'dd.mm.yy',
               onSelect: function(dateText) {
                   var modelPath = $(this).attr('ng-model');
                   putObject(modelPath, scope, dateText);
                   scope.$apply();
               }
           });
       }
    });

    然后在 HTML 中引入这个 directive

     <input type="text" datepicker ng-model="myObject.myDateValue" />

     
  • 相关阅读:
    java 代码规范 sun 公司
    软引用、弱引用、虚引用
    socket
    httpURLConnection、URL、httpClient、httpPost、httpGet
    android service aidl 理解
    Python2.7-codecs
    Python2.7-textwrap
    Python2.7-StringIO和cStringIO
    Python2.7-difflib
    Python2.7-struct模块
  • 原文地址:https://www.cnblogs.com/toward-the-sun/p/4870378.html
Copyright © 2011-2022 走看看