gpt4 book ai didi

javascript - AngularJS 错误 ng areq 不是函数

转载 作者:行者123 更新时间:2023-11-27 23:42:47 25 4
gpt4 key购买 nike

我试图让我的元 Controller 动态更改元标记,但在控制台中我收到错误 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并且您没有自己定义它。但是,看起来您只需要 accecc 当前路由 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

关于javascript - AngularJS 错误 ng areq 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com