无法在routeprovider中注入控制器

时间:2016-05-09 15:06:59

标签: javascript angularjs angularjs-routing

我仍然在angularJS做一个execise以提高我的知识,但现在我正在练习路线,我无法让控制器属性在路由模板中工作。我已经阅读了很多教程,它现在应该可以正常工作所以我正在粘贴代码片段。

的Javascript

angular.module('greetings', ['ngRoute'])
.service("Addcontent", function($http, $q){
     this.name = "Nome";
     this.familyName = "Cognome";
     me = this;
     me.tipi= [];
     this.tipi = me.tipi;
     this.selectedOption = this.tipi[0];
     this.getTipi = function() {
         var deferred = $q.defer();
            $http.get("http://localhost:8090/my-site/data.json").success(function(data) {
                  me.tipi = data;
                 me.selectedOption = me.tipi[0];
                 deferred.resolve( {options: me.tipi, selectedOption: me.selectedOption} );
             }).error(function(){
                 console.log("error");
             });
         return deferred.promise;
     };
}).controller("renameAddContentController", function($scope, $window, $http, Addcontent, $q, $routeParams, $route){
      this.name = Addcontent.name;
      console.log(this.name);
      this.familyName = Addcontent.familyName;
      consolelog(this.familyName);
      var ctrl = this;
        Addcontent.getTipi().then(function(data) {
            ctrl.tipi = data.options;
        });
        ctrl.selectedOption = $routeParams.showName;
        console.log(ctrl.selectedOption);
}).config(function ($routeProvider, $locationProvider){
  $routeProvider
    .when('/greeted/:showName',
     {
        controller:'controllers.renameAddContentController',
        controllerAs: 'rename',
        templateUrl:'/my-site/greeted.html'
     })
    .otherwise({ redirectTo: '/' })
    $locationProvider.html5Mode(true);
});

正如您所看到的,有一个服务,一个控制器和一个可以找到的路径。模板非常简单:

<h3>Greeting:</h3>
<span>
  {{rename.selectedOption}} {{rename.name}} {{rename.familyName}}
  <br/>
</span>

主HTML也很简单:

  <a href="/my-site/greeted/{{greeting.selectedOption}}">Greet</a>
  <span ng-view>
    </span>

(问候语属于此示例中未包含的其他控制器)...

错误说控制器不是函数,因此它不解析模板中提到的属性..

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您的控制器名称:

$routeProvider
    .when('/greeted/:showName',
     {
        controller:'controllers.renameAddContentController',
        controllerAs: 'rename',
        templateUrl:'/my-site/greeted.html'
     })

应该只是您之前定义的renameAddContentController,而不是controllers.renameAddContentController