$ scope在我的导航栏控制器中不起作用?

hap*_*der 5 javascript twitter-bootstrap angularjs angularjs-scope angularjs-routing

我的Angular应用程序是使用这个yeoman发生器的样板开发的.

路由和所有工作正常但我无法在navbar-controller.js和footer-controller.js上工作$ scope.请告诉我您是否需要更多信息来提供相关信息.

的index.html

<!DOCTYPE html>
<html lang='en' data-ng-app='app'>
  <head>
    <title>App</title>
    <meta name='viewport' content='width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes'>
    <!-- inject:head:js -->
    <!-- endinject -->
    <!-- inject:html -->
    <!-- endinject -->
    <!-- bower:css -->
    <!-- endbower -->
    <!-- inject:css -->
    <!-- endinject -->
  </head>
  <body class="wrapper">

    <div ng-controller="NavbarCtrl" ng-include="'navbar/navbar.tpl.html'"></div>

    <!-- boxed-layout  -->
    <div class='container'>

      <!--=== Main Content ===-->
      <div data-ui-view></div>
      <!--=== End Main Content ===-->

    </div>

    <!-- <div id="footer"
        ng-controller="FooterCtrl" ng-include="'footer/footer.tpl.html'">
    </div> -->

    <!-- bower:js -->
    <!-- endbower -->
    <!-- inject:js -->
    <!-- endinject -->
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

导航栏,controller.js

(function () {
  'use strict';

  angular
    .module('navbar')
    .controller('NavbarCtrl', NavbarCtrl);

  function NavbarCtrl() {
    var vm = this;
    vm.ctrlName = 'NavbarCtrl';

    this.loginHeader = function(){
      console.log("LOGIC called");
    }
  }
}());
Run Code Online (Sandbox Code Playgroud)

导航栏,module.js

(function () {
  'use strict';
  angular
    .module('navbar', [
      'ui.router'
    ]);
}());
Run Code Online (Sandbox Code Playgroud)

导航栏,routes.js

(function () {
  'use strict';

  angular
    .module('navbar')
    .config(config);

  function config($stateProvider) {
    $stateProvider
      .state('navbar', {
        url: '/navbar',
        templateUrl: 'navbar/navbar.tpl.html',
        controller: 'NavbarCtrl',
        controllerAs: 'navbar'
      });
  }
}());
Run Code Online (Sandbox Code Playgroud)

导航栏,trl.html

....
.....
<li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Search</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li ng-hide="navbar.isLoggedIn"><a ui-sref="login">Login {{navbar.ctrlName}}</a></li>
        <li ng-hide="navbar.isLoggedIn" ><a ui-sref="register">Signup</a></li>
        <li ng-show="navbar.isLoggedIn"><a>Logged as {{navbar.username}}</a></li>
        <li ng-show="navbar.isLoggedIn"><a ui-sref="login">Logout</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
....
.....
Run Code Online (Sandbox Code Playgroud)

更新: 我找到了答案.谢谢大家.我想知道在其他控制器中我没有注入$ scope,因为我使用控制器作为语法,而且它工作正常.任何人都可以解释这背后的原因,为什么只有在navbar我需要注入$ scope?

Dam*_*ano 3

你必须像这样初始化并注入 $scope :

(function(){
    'use strict';
    angular.module('navbar')
    .controller('NavbarCtrl', ['$scope', 'otherDependecy', function($scope, otherDependecy){
        yourAwesomeCodeGoesHere();
        $scope.someObject = 'Hello world';
    }]);
})();
Run Code Online (Sandbox Code Playgroud)

或者:( 只提取控制器的主要功能)

(function(){
    'use strict';
    angular.module('navbar')
    .controller('NavbarCtrl', ['$scope', 'otherDependecy', NavbarCtrl]);

    function NavbarCtrl($scope, otherDependecy){
        yourAwesomeCodeGoesHere();
        $scope.someObject = 'Hello world';
    }

})();
Run Code Online (Sandbox Code Playgroud)

@更新:

您可能指的是指令控制器?在这种情况下, $scope会自动初始化并注入,您只需记住将$scope作为参数添加到控制器的函数中即可。就这样。