加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用$routeParams时angular.js模块化控制器“undef

发布时间:2020-12-17 08:46:00 所属栏目:安全 来源:网络整理
导读:以下命令在控制台中返回错误“ReferenceError:未定义ThingCtrl” var myApp = angular.module('myApp',[]);myApp.config(['$routeProvider',function($routeProvider) {$routeProvider. when('/things',{templateUrl: 'partial.html',controller: ThingCtrl
以下命令在控制台中返回错误“ReferenceError:未定义ThingCtrl”
var myApp = angular.module('myApp',[]);

myApp.config(['$routeProvider',function($routeProvider) {
$routeProvider.
    when('/things',{templateUrl: 'partial.html',controller: ThingCtrl}).
    when('/things/:id',{templateUrl: 'detail.html',controller: ThingCtrl}).
otherwise({redirectTo: '/things'});
 }]);

myApp.controller('ThingCtrl',['$scope','$routeParams',function($scope,$routeParams) {
$scope.thing = [
    {
        'title':'first thing','first':'one','second': 'two','third': 'three'
    }
];

}]);

但是当控制器被定义为:

function ThingCtrl($scope,$routeParams) {
        $scope.thing = [
    {
        'title':'first thing','third': 'three'
    }
  ]
};

为什么使用模块化语法不起作用?

我相信这个问题在这里:
when('/things',controller: ThingCtrl})

这告诉Angular指向ThingCtrl对象,该对象未定义并导致错误.

尝试将控制器名称括在引号中,如下所示:

when('/things',controller: 'ThingCtrl'})

这应该允许Angular正确使用依赖注入.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读