$ scope不适用于我的angularjs应用程序

时间:2013-12-11 15:24:14

标签: javascript angularjs

无法在我的视图中显示yo homie,并且为什么会这么做。

这里是一个github回购https://github.com/homiepotato/-scope-not-working

注意:这是我的主页,我的所有javascript lib都位于

我的观点(master.html)

 <html ng-app='potato'>
<div ng-controller="MasterCtrl">
<div id='menu' class="pure-u-1">
    {{s_master}}     
</div>
</div>
<div ng-view=''></div> <!-- this is where main.html inject-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular-route.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular-cookies.js"></script>
<script type="text/javascript" src="/static/javascripts/app.js"></script>
<script type="text/javascript" src="/static/javascripts/controllers.js"></script>

</html>

/static/javascripts/app.js

   var potato = angular.module('potato',['ngRoute','ngCookies']).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
    templateUrl: '/static/views/main.html',
    controller: 'IndexCtrl'
  }).
  when('/login',{
    templateUrl: '/static/views/login.html',
    controller: 'LoginCtrl'
  }).
  when('/register',{
    templateUrl: '/static/views/register.html',
    controller: 'RegisterCtrl'
  })
      /* some route and controller but the MasterCtrl is not  included */
    $locationProvider.html5Mode(true);
  }]);

/static/javascripts/controllers.js

 // controller for master.html(the master page)
function MasterCtrl($scope, $http, $cookies) {
  $scope.s_master = "yo homie" 
 /* the $scope is not working on this controller
    but other function work fine like $cookies $http
    the rest of the controller work fine but not MasterCtrl 
 */
}
// controller for main.html
function IndexCtrl($scope, $http) {

}
// controller for login.html
function LoginCtrl($scope, $http, $location) {
  $scope.form = {};
  $scope.errorMessage='';
  $scope.submitLogin = function() {
    $http.post('/login',$scope.form).
      success(function(data){
        $location.path('/');
      }).error(function(err){
        $scope.errorMessage = err;
      });
  }
}
// controller for register.html
function RegisterCtrl($scope, $http, $location) {
    $scope.form = {};
    $scope.errorMessage = '';
  $scope.submitPost = function() {
    $http.post('/register', $scope.form).
      success(function(data) {
        $location.path('/');
      }).error(function(err) {
        $scope.errorMessage = err;
      });
  };
}

5 个答案:

答案 0 :(得分:0)

除非您没有显示所有代码,否则您缺少“ng-app”。

<div ng-app ng-controller="MasterCtrl">

答案 1 :(得分:0)

ng-app 属性在哪里?

您的HTML应如下所示:

<div ng-app="MyApp" ng-controller="MasterCtrl">

  <div id="menu" class="pure-u-1">
     {{s_master}}
  </div>

</div>

和Javascript:

  var app = angular.module('MyApp', []);

  app.controller('MasterCtrl', function ($scope) {

    $scope.s_master = "yo homie";

  });

答案 2 :(得分:0)

你发布的代码没有任何问题,我把它放在http://plnkr.co/edit/D7qpi4j4gYn2l4Kqd74X?p=preview,它按照你的预期运行。

也许你的代码的另一部分没有发布导致问题?

答案 3 :(得分:0)

你的app.js几乎完全被打破了。你有:

  • $routeProvider函数调用
  • 未提及when
  • 最终.
  • 末尾的when

您应该会在JavaScript控制台中看到一些错误。

尝试更正您的app.js以阅读以下内容:

 var potato = angular.module('potato', ['ngRoute','ngCookies']).
 config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
      templateUrl: '/static/views/main.html',
      controller: 'IndexCtrl'
    }).
    when('/login',{
      templateUrl: '/static/views/login.html',
      controller: 'LoginCtrl'
    }).
    when('/register',{
      templateUrl: '/static/views/register.html',
      controller: 'RegisterCtrl'
    })

    /* some route and controller but the MasterCtrl is not  included */
    $locationProvider.html5Mode(true);
  }]);

正如@DumitruChirutac建议的那样,我还建议您不要使用控制器的“全局”命名,而是在您的应用模块上定义它们。

在这里工作:http://plnkr.co/edit/gcieHTrw50AnWPZVzQNL?p=preview

答案 4 :(得分:0)

检查了由

引起的代码
app.get('*',function(req, res){
  res.render('master');
});

因为res.render客户端无法获取$ scope值,只需将其更改为

app.get('*',function(req, res){
  res.sendfile('./public/views/master.html');
});