簡體   English   中英

內聯數組注釋DI是否與AngularJS中的$ routeProvider兼容?

[英]Isn't Inline Array Annotation DI compatible with $routeProvider in AngularJS?

我的代碼看起來像這樣

angular
.module('main', ['ngRoute'])
.config(['$routeProvider',
    ($routeProvider) ->
    $routeProvider
    .when '/',
      templateUrl: 'homePage/homePage.html'
      controller: 'MainCtrl'
])

angular.module('main').controller('MainCtrl',
 ['$scope' , ($scope) ->
    $scope.test = {}])

瀏覽器將顯示Error: [ng:areq] Argument 'MainCtrl' is not a function, got Object

但是,如果我不在MainCtrl使用內聯數組依賴項注入並像這樣重寫它:

angular.module('main').controller('MainCtrl',
 ($scope) ->
    $scope.test = {})

然后一切正常。 有人對此有想法嗎? 謝謝!

由於錯誤消息很明顯,因為問題不在$routeProvider ,您可能需要對其進行重組。 另請注意,配置塊必須具有功能。

首先創建模塊,然后注冊控制器和配置:

angular
.module('main', ['ngRoute']);

然后使用它或通過鏈,即

angular.module("main", ["ngRoute"]).controller("MainCtrl", [
  "$scope"
  ($scope) ->
    return $scope.test = {}
]).config [
  "$routeProvider"
  ($routeProvider) -> //Check this
    return $routeProvider.when("/",
      templateUrl: "homePage/homePage.html"
      controller: "MainCtrl"
    )
]

否則,按照腳本的順序,您嘗試在應用程序main界面上創建一個控制器,甚至不存在。

還要注意,您還需要包括angular-router腳本。

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM