2015-11-04 57 views
1

我试图让我的元控制器动态更改元标记,但在控制台中我得到error ng areq not a function。我通过StackOverflow搜索类似的问题,但没有解决方案是为我的问题。我在我的HTML,这些标签:AngularJS错误代码不是函数

<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; 
}); 
+0

你能把metadataService –

+0

的代码,我没有任何代码它。我在网上编译了各种教程的代码。我是否需要代码,我认为角处理元数据服务。 – user3187715

+0

你的构建过程是什么?你是否缩小了你的代码? – LordTribual

回答

1

有没有这样的服务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

+0

是的,但我需要将它们绑定到标题,描述等,并让谷歌机器人在获取标签中看到它们。这就是为什么我选择这种方式来改善搜索引擎优化。 – user3187715

+0

那么问题是什么:'$ scope.meta = $ route.current.metadata'。 – dfsq

+0

我写了它,但它并没有改变任何东西。我需要额外的东西吗? – user3187715