jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将jQuery插件输入值推送到AngularJS中的模型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
情况:
我正在移植,或者我应该说尝试将Lakshan Perera的简单jQuery ColorPicker(https:// github.com/laktek/really-simple-color-picker)移植到Angular上.在回顾SO上的类似问题后,似乎有些人通过控制器分配了插件的范围,但正确的(Angular)方法是将插件包装在指令中.我越来越近了.我能够通过我的新自定义属性在我的视图中正确呈现插件,但我不确定如何设置指令以将输入值传递给属性的值(ng-model).实际的输入更新,但Angular没有监听更改,因此实际上并不知道输入值已更新.官方文档讨论了如何设置自定义属性(http://docs. angularjs.org/guide/directivE),但我仍然无法弄清楚如何实际观察元素中的更改然后将该值推送到属性的值.

期望的功能

<input my-widget="{{Color}}" type="text"/> <!-- my-widget instantiates my directive -->
<h1 style="color:{{Color}}"></h1>          <!-- I would like the input value to dump into the attribute's value,in this case {{Color}} -->

这是我到目前为止:

app.directive('myWidget',function(){
    var myLink = function(scope,element,attr) {

        scope.$watch('element',function(){

            var value = element.val();

            element.change(function(){

             console.log(attr.ngModel); // This is currently logging undefined

                     // Push value to attr here? 

                console.log( value + ' was SELEcted');
            });
        });

        var element = $(element).colorPicker();
    }

    return {
        reStrict:'A',link: myLink
    }   

});

题:
我如何设置属性值来捕获元素的更新值?

解决方法

我会像这样实现它:

app.directive('colorPicker',function() {
  return {
    scope: {
      color: '=colorPicker'
    },link: function(scope,attrs) {
      element.colorPicker({
        // initialize the color to the color on the scope
        pickerDefault: scope.color,// update the scope whenever we pick a new color
        onColorChange: function(id,newvalue) {
          scope.$apply(function() {
            scope.color = newValue;
          });
        }
      });

      // update the color picker whenever the value on the scope changes
      scope.$watch('color',function(value) {
        element.val(value);
        element.change();                
      });
    }
  }
});

你会这样使用它:

<input color-picker="color">

这是一个工作的jsfiddle,有几个小小的玩具:http://jsfiddle.net/BinaryMuse/x2uwQ/

jsfiddle使用隔离范围将范围值颜色绑定到传递给颜色选择器属性的任何颜色.我们观察表达式’color’以查看此值何时更改,并适当更新颜色选择器.

大佬总结

以上是大佬教程为你收集整理的将jQuery插件输入值推送到AngularJS中的模型全部内容,希望文章能够帮你解决将jQuery插件输入值推送到AngularJS中的模型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。