javascript - 如何将 Controller 配置移至其自己的文件中?

标签 javascript angularjs ionic-framework

我在文件index.js中有以下 Angular 配置:

angular.module('ionicApp', ['ionic'])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

        $stateProvider
            .state('entry', {
                url: '/entry',
                templateUrl: 'app/views/entry/entry.html',
                controller: 'EntryPageController'
            })


        $urlRouterProvider.otherwise('/entry');
    }])

    .controller('EntryPageController', ['$scope', '$state', function ($scope, $state) {
        $scope.navTitle = 'Entry Page';

        $scope.signIn = function () {
            $state.go('main.home');
        }
    }])

我正在尝试将 Controller 定义(在上面的示例中起作用)移动到它自己的文件中,如下所示:

// file name entry-ctrl.js
(function () {
    'use strict';

    angular.module('ionicApp', ['ionic'])
        .controller('EntryPageController', ['$scope', '$state', EntryPageController]);

    function EntryPageController($scope, $state) {
        $scope.navTitle = 'Entry Page';

        $scope.signIn = function () {
            $state.go('main.home');
        }
    }
})
();

在index.html中,我将该文件引用为

<script src="app/views/entry/entry-ctrl.js"></script>

不幸的是,我无法让应用程序正常运行。当我使用原始代码时,页面会按我的预期显示。但是当我使用entry-ctrl.js文件中的代码时,什么也没有出现。

我还需要做些什么才能使用entry-ctrl.js 文件中的代码吗?

郑重声明,这是我的entry.html:

<ion-view title="{{navTitle}}" class="bubble-background">
    <ion-content has-header="true" padding="true">
        <h1>I'm working!</h1>
        <a class="button button-positive" ng-click="signIn()" ui-sref="main.home">Sign In</a>
    </ion-content>

</ion-view>

最佳答案

您似乎声明了两次 Angular 应用程序,一次在 index.js 中,一次在 entry-ctrl.js 中。

我会把它改成这样:

index.js

angular.module('ionicApp', ['ionic'])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

        $stateProvider
            .state('entry', {
                url: '/entry',
                templateUrl: 'app/views/entry/entry.html',
                controller: 'EntryPageController'
            })


        $urlRouterProvider.otherwise('/entry');
    }])

entry-ctrl.js

(function () {
    'use strict';

    angular.module('ionicApp')
        .controller('EntryPageController', ['$scope', '$state', EntryPageController]);

    function EntryPageController($scope, $state) {
        $scope.navTitle = 'Entry Page';

        $scope.signIn = function () {
            $state.go('main.home');
        }
    }
})();
<小时/>

在 Angular 中,您可以使用一系列依赖项来声明您的应用程序:

angular.module('ionicApp', ['ionic'])

并且您仅通过名称引用它:

angular.module('ionicApp')

关于javascript - 如何将 Controller 配置移至其自己的文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35768781/

相关文章:

javascript - (不是这样)聪明的 key 导致 Node JS 中的 SHA512 Hmac 出现问题

javascript - 类 'B' 错误地扩展了基类 'A'(两个私有(private)方法命名相同)

javascript - Angular JS : How can i fire ng-blur on input field as soon as is displayed?

javascript - 将图标添加到正确/不正确的答案(ionic 中的 css)

javascript - 防止 jquery 在表中追加重复值

javascript - ui-bootstrap-tpls 和 ui-bootstrap

javascript - Ng-Click 未被触发

ios - Cordova 相机 iOS 问题 : NOT_FOUND_ERR

ios - ionic emulate ios - 不会模拟并说没有这样的文件或目录

javascript - 如何访问 AngularJS 指令元素索引