AngularJS错误ng-areq不是函数

AngularJS error ng areq not a function

本文关键字:函数 ng-areq 错误 AngularJS      更新时间:2023-09-26

我试图让我的元控制器动态更改元标记,但在控制台中我得到了error ng areq not a function。我在StackOverflow中搜索了类似的问题,但没有一个解决方案适合我的问题。我的HTML中有这些标签:

    <html  ng-app="WebApp" >
    <head  ng-controller="MetaDataCtrl">
    <meta name="description" content="{{ meta.tags.description }}">
</head>
<body >
    <div ng-include='"templates/header.html"'></div>
    <div ng-view></div>
</body>
</html>

Main.js

var app = angular.module('WebApp', [
  'ngRoute'
]);
/**
 * Configure the Routes
 */
app.config(['$routeProvider', '$locationProvider', function($routes, $location) {
 $location.html5Mode(true).hashPrefix('!');
  $routes
    // Home
    .when("/", {templateUrl: "partials/home.html",  
      controller: "PageCtrl",
      metadata: {
           title: 'This is my title',
           description: 'This is Desc.' }
    })
}]);
app.controller('PageCtrl', function (/* $scope, $location, $http */) {
});
.controller('MetadataCtrl', function ($scope, metadataService) {
   $scope.meta = metadataService;
});

没有这样的服务metadataService,您自己也没有定义它。然而,看起来您只需要访问当前路由metadata对象。在这种情况下,这很容易做到,因为它是$route服务的一部分。您还应该设置一个侦听器,以便在路由更改时更新全局meta对象:

app.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function(event, current) {
        $rootScope.meta = current.metadata;
    });
}]);

演示:http://plnkr.co/edit/nQfqNWvoYQQElv909uZF?p=preview