AngularJs服务未注入控制器

时间:2015-10-03 01:25:01

标签: angularjs dependency-injection angular-services angular-digest

我遇到一些非常简单的问题,比如将一个基本服务注入控制器,尽管阅读文档和其他问题我真的没有看到我正在做的错误。

这是我的简化代码:

angular.module ('travelApp', ['ui.bootstrap', 'ngRoute'])

// CONTROLLERS

.controller('searchController', ['$scope','myParams', function ($scope,ngRoute,myParams){

   $scope.place = myParams.place;
   $scope.$watch('place', function(){
      myParams.place = $scope.place;
    });

    $scope.bind = function(){
        console.log(myParams.getPlace);
    };
}])

// SERVICES

.service('myParams', function(){
    this.place = 'start';
});

//我的HTML

<div class="container" ng-app="travelApp">
    <div class="jumbotron" id="searchPage" ng-controller="place">
    <div>
        <h3>Start Here</h3>
        <input type="text" class="form-control" id="searchBar" ng-model="place">
        <a type="button" class="btn btn-default" ng-click="bind()">bind data</a>
    </div>
</div>

    

在过去的几个小时里我尝试了很多东西,但一直在努力

“TypeError:无法设置未定义的属性'place'”

似乎该服务在控制器内部无法使用,我不知道它是否与路由有关,但我对此表示怀疑。

有人可以指出我正确的方向吗?

这是Plunker

1 个答案:

答案 0 :(得分:2)

如果使用数组语法,则依赖项必须匹配。从控制器的参数中删除ngRoute

.controller('searchController', ['$scope','myParams', function ($scope, myParams){