jquery函数转为angular指令实现示例

最初像想要实现的是:显示如下的小星星

最初的实现方式是在html页面加JS脚本,但是老外不喜欢这样,一定要我们转为angular指令,所以就试试呗~

一、最初的实现方式

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta content="width=device-width; initial-scale=1; maximum-scale=1" name="viewport">
<title>jquery评分插件jquery.raty</title>

<link type="text/css" rel="stylesheet" href="demo/css/application.css">
<script type="text/javascript" src="demo/js/jquery.min.js"></script>
<script type="text/javascript" src="lib/jquery.raty.min.js"></script>
</head>
<body>
<div style="width:500px; margin:100px auto;">

    <div id="function-demo1" class="target-demo"></div>

</div>

<script type="text/javascript">
    $(function() {
      $.fn.raty.defaults.path = 'lib/img';

	  $('#function-demo1').raty({
	  	number: 5,//多少个星星设置
		score: 3,//初始值是设置
		targetType: 'number',//类型选择,number是数字值,hint,是设置的数组值
        path      : 'demo/img',
        cancelOff : 'cancel-off-big.png',
        cancelOn  : 'cancel-on-big.png',
        size      : 24,
        starHalf  : 'star-half-big.png',
        starOff   : 'star-off-big.png',
        starOn    : 'star-on-big.png',

        cancel    : false,
        targetKeep: true,
        precision : false,//是否包含小数

      });
    });

  </script>

二、改成angular指令形式(使用了requireJS)

说明:我这里使用了第三方的插件jquery.raty.min.js(评分插件),它是要依赖于jquery的。

directive.js

//stars
define([ 'angular','raty' ], function(angular) {

    var directives = angular.module('directives', []);

    directives.directive('showStars', function() {
        return {
            restrict : 'A',
            controller : [ '$scope', '$element', '$timeout', function($scope, $element, $timeout) {
                $timeout(function() {

                    $.fn.raty.defaults.path = 'img';

                    $($element).raty({
                        number : 5,
                        score : 3,
                        half : false,
                        size : 30
                    });

                }, 100);
            } ]
        };
    });
    return directives;
});

三、RequireJS中一般都有个main.js的文件作为启动点,这里也不例外

main.js

//配置依赖
require.config({
    paths: {
      "angular":"angular",
      "jquery":"jquery.min",
      "raty":"jquery.raty.min"
    },
    shim:{
        "angular": {
            "deps":[],
            "exports": "angular"
        },
        "jquery":{
            "deps":[],
            "exports": "jquery"
        },
        "raty":{
            "deps":["jquery"],
            "exports": "raty"
        }
    }
  });

//手动启动对应模块
require([ 'angular', 'controller', 'directive' ], function(angular) {
    angular.bootstrap(document, [ 'HelloCtrls', 'directives']); 

});

注意:在这里我使用的是手动启动的方式,html页面中去掉ng-app指令 ,改为自动启动如下:

define([
        'angular',
        'controller',
        'directive'
        ], function (angular) {    

        angular.module('HelloModel', ['HelloCtrls','directives']);

        return {
            angularModules: [ 'HelloModel' ]
        };    

    });

但,不幸的是,改为自启动的时候总是说无法实例化HelloModel(页面中我加了ng-app="HelloModel")我怀疑是js文件的加载顺序,但是后来发现么有什么问题啊,特别纠结,现在仍然未能解决,如果有人能告诉我原因不胜感激!

四、页面中使用上述自定义指令

index.js

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<script src="js/require.js" data-main="js/main" defer async="true" ></script>
</head>
<body ng-controller="helloCtrl">
{{greet}}
<div show-stars ></div>
</body>
</html>

五、说明

上述的控制器与该指令无关,我就是测试用的,当然还是贴出来好些。

controller.js

define([ 'angular' ], function(angular) {

    var componentCtrls = angular.module('HelloCtrls', []);

     componentCtrls.controller('helloCtrl', [ '$scope',function($scope) {

        $scope.greet = "hello world";

      } ]);

    return componentCtrls;
});

难道是使用了第三方才插件的缘故就只能改为手动启动吗?求解。。。。

时间: 2024-08-22 04:24:39

jquery函数转为angular指令实现示例的相关文章

Angular指令封装jQuery日期时间插件datetimepicker实现双向绑定

一放假就高产似母猪了. 00.混乱的前端界 Angular1.x确实是个学习成本很高的框架,刚开始实习那会儿,前端啥也不懂,工头说用Angular,我们这群小弟也只能硬着头皮学.在这之前,前端的东西大部分都用的jQuery,而Angular正好是和jQuery的思维是相反的,开发过程中遇到了不少坑.而Angular团队也放弃了1.x开始开发和React神似的2.0版本,唉,真是沧海桑田啊. 01.Angular vs jQuery Angular模块化和解耦的思路确实值得一学,但是相对于成熟的j

如何编写Angular指令

[20140917]Angular:如何编写一个指令 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre

【转】angular指令入坑

独立作用域和函数参数 通过使用本地作用域属性,你可以传递一个外部的函数参数(如定义在控制器$scope中的函数)到指令.这些使用&就可以完成.下面是一个例子,定义一个叫做add的本地作用域属性用来保存传入函数的引用: angular.module('directivesModule').directive('isolatedScopeWithController', function () {    return {        restrict: 'EA',        scope: { 

PHP jQuery ajax 表单提交小示例(含insert, select)

功能描述:能够通过表单向MySQL数据库新增记录,能够表单提供关键词进行查询 index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&g

java jquery 函数多参数传递

业务需求: 名次   伙伴 业绩 签单 面谈 每日目标 1 文彬 5100 6 10 查看目标 2 马红月 4550 4 6 查看目标 3 王刚 4100 3 9 查看目标 4 郭亚凯 3450 4 9 查看目标 5 王洪良 3300 3 7 查看目标 6 袁凯 2200 2 4 查看目标 7 王兆阳 2100 3 6 查看目标               每日目标    文彬 2014-06-13    15号前 争取业绩到达6000以上...    加油 加油 2014-06-12昨天打了1

jQuery Mobile的布局插件和示例

[转自网络] 现在已经进入了移动互联网时代,因此将你的网站迁移到移动设备上就显得比较重要的.问题是,如何在移动设备的小屏幕中呈现你的网站中的所有内容呢? 本文介绍13款基于jQuery Mobile的布局插件和示例,可以帮助你创建多视图或者分割视图布局的移动web页面,并会根据移动设备的方向和屏幕大小来动态调整所显示的内容. 1.  Three Column iPad Layout 三列分割视图布局效果,为手机和平板电脑上的移动网页布局提供了一个良好的平台. 源码/ 演示 2.  JQM Mul

PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)

转载 http://justcoding.iteye.com/blog/601117/ //定义编码header( 'Content-Type:text/html;charset=utf-8 '); //Atomheader('Content-type: application/atom+xml'); //CSSheader('Content-type: text/css'); //Javascriptheader('Content-type: text/javascript'); //JPEG

枚举光标当前所在位置的函数包含的指令的数量

//枚举光标当前所在位置的函数包含的指令的数量 // #include <idc.idc> static main() { auto func,end,count,inst; func = GetFunctionAttr(ScreenEA(),FUNCATTR_START); //获取包含光标位置的起始地址 if(func != -1) { end = GetFunctionAttr(func,FUNCATTR_END); //获取函数的结束地址 count = 0; inst = func;

Asp.net中固定位数用零补齐的函数(已解决,示例)!

在开发当中,出现编号实现8位数,但需要用0补齐.如:123,表示:0000123. 实例如下: decimal aaa = 123; // 数值型 string bbb = aaa.ToString(); // 转换为字符 bbb = bbb.PadLeft(7, '0'); // 共7位,之前用0补齐 response.write(bbb ); decimal aaa = 123;    // 数值型 string bbb =  aaa.ToString();   // 转换为字符 bbb =