如何使用requirejs绑定角度js中的click事件?

时间:2015-06-26 10:39:01

标签: angularjs angularjs-directive requirejs angularjs-scope ionic-framework

请告诉我如何使用require js + angularjs

绑定click事件

我尝试绑定click事件,但它绑定了click事件 Here is my code

的index.html

<!DOCTYPE html>
<html>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<head>
    <link type="text/css" href="http://code.ionicframework.com/1.0.0-beta.1/css/ionic.css" rel="stylesheet" />
</head>

<body>
<ion-nav-view animation="slide-left-right"></ion-nav-view>
<script data-main="main" src="lib/require.js"></script>
</body>

</html>

main.js

requirejs.config({
    paths: {
        ionic:'lib/ionic.bundle'
    },
    shim: {
        ionic : {exports : 'ionic'}
    }, priority: [
        'ionic'
    ],

    deps: [
        'bootstrap'
    ]
});

bootstrap.js

/*global define, require, console, cordova, navigator */

define(['ionic',  'app', 'routes'], function (ionic, angular, app) {
    'use strict';

    var $html,
        onDeviceReady = function () {
            angular.bootstrap(document, [app.name]);
        };

    document.addEventListener("deviceready", onDeviceReady, false);

    if (typeof cordova === 'undefined') {
        $html = angular.element(document.getElementsByTagName('html')[0]);
        angular.element().ready(function () {
            try {
                angular.bootstrap(document, [app.name]);
            } catch (e) {
                console.error(e.stack || e.message || e);
            }
        });
    }

});

app.js

/*global define, require */

define(['ionic'],

    function (angular) {
        'use strict';

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

        return app;

    });

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误...太多也列出了所有错误。有关更正,请参阅Plunker

  1. 模块ionic应导出为angular

    requirejs.config({
        paths: {
            ionic:'lib/ionic.bundle'
        },
        shim: {
            ionic : {exports : 'angular'}
        }, priority: [
            'ionic'
        ],
    
        deps: [
            'bootstrap'
        ]
    });
    
  2. 必须首先加载离子/角度 - 或者角度不能初始化。

  3. 在路线中,您必须按名称引用控制器 - 而不是按位置

    /*global define, require */
    
    define(['app'], function (app) {
    'use strict';
    
    
    app.config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
    
            $stateProvider
                .state('login', {
                    url: "/login",
                    templateUrl: "login.html",
                    controller: 'controllers/LoginCtrl' // <-- should be 'LoginCtrl'
                })
    
            $urlRouterProvider.otherwise("/login");
    
        }]);       
    });