Implementation example of converting jquery function into angular command, jqueryangular

Source: Internet
Author: User

Implementation example of converting jquery function into angular command, jqueryangular

The first thing you want to achieve is: show the following stars

The initial implementation method was to add JS scripts to html pages, but foreigners do not like this. We must convert them into angular commands, so try again ~

I. Initial Implementation Method

<! DOCTYPE html> 

Ii. Change to angular command form (requireJS is used)

Note: I used a third-party plug-in jquery. raty. min. js (scoring plug-in), which depends on jquery.

Directive. js

//starsdefine([ '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;});


3. RequireJS generally has a main. js file as the startup point, which is no exception here

Main. js

// Configure the dependency 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 "}}}); // manually start the require (['angular ', 'controller', 'ctive Ve'], function (angular) {angular. bootstrap (document, ['helloctrls', 'ctives ves ']);});

Note: Here I am using the manual startup method. Remove the ng-app command from the html page and enable it as follows:

define([        'angular',        'controller',        'directive'        ], function (angular) {                  angular.module('HelloModel', ['HelloCtrls','directives']);        return {            angularModules: [ 'HelloModel' ]        };                });

However, unfortunately, it is always said that the HelloModel cannot be instantiated when it is started by itself (ng-app = "HelloModel" is added to the page). I suspect it is the loading sequence of js files, however, what problems did I find? I am deeply troubled and still cannot solve them. If someone can tell me why, I am very grateful!


4. Use the preceding custom commands on the page

Index. js

<!DOCTYPE html>

V. Description

The above controller has nothing to do with this command. I used it for testing. Of course it is better to post it.

Controller. js

define([ 'angular' ], function(angular) {    var componentCtrls = angular.module('HelloCtrls', []);     componentCtrls.controller('helloCtrl', [ '$scope',function($scope) {        $scope.greet = "hello world";      } ]);    return componentCtrls;});


Is it because a third-party plug-in is used, it can only be started manually? Solving ....



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.