【问题标题】:Angular.js - not enter inside a controllerAngular.js - 不进入控制器内部
【发布时间】:2018-04-04 01:20:56
【问题描述】:

为什么不输入AddTaskComponentController 并显示我的console.log('TESTE456214651')?代码在同一个存档中,因为我使用的是 grunt。

(function() {
    'use strict';

    angular.module('dailyTasksApp', []).config(config)

    function config($routeProvider, $locationProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });

        $routeProvider.when('/', {
            templateUrl: 'modules/construction-employees-daily-tasks/app/app.component.html'
        }).when('/adicionar-tarefa', {
            templateUrl: 'modules/construction-employees-daily-tasks/add-task-component/add-task.component.html'
        })
    }
})();

(function(){
    'use strict';

    angular.module('dailyTasksApp').component('addTaskComponent', {
        templateUrl: '/modules/construction-employees-daily-tasks/add-task-component/add-task.component.html',
        controller: AddTaskComponentController
    })

    function AddTaskComponentController() {
        console.log("TESTE456214651");
    }
})();

【问题讨论】:

  • 你永远不会调用这个函数。它只是声明的。

标签: javascript angularjs iife


【解决方案1】:

你应该将你的函数声明为一个 Angular 控制器。

(function(){
    'use strict';

    angular
        .module('dailyTasksApp', [])
        .config(config)

    function config($routeProvider, $locationProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });

        $routeProvider
            .when('/', {
                templateUrl: 'modules/construction-employees-daily-tasks/app/app.component.html'
            })
            .when('/adicionar-tarefa', {
                templateUrl: 'modules/construction-employees-daily-tasks/add-task-component/add-task.component.html'
            })
    }
})();

(function(){
    'use strict';

    angular
        .module('dailyTasksApp')
        .controller('AddTaskComponentController',
           function(){
               console.log("TESTE456214651");
           })
        .component('addTaskComponent', {
            templateUrl: '/modules/construction-employees-daily-tasks/add-task-component/add-task.component.html',
            controller: AddTaskComponentController
        })

})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2016-04-29
    • 2021-05-01
    相关资源
    最近更新 更多