无法在我的视图中显示yo homie
,并且为什么会这么做。
这里是一个github回购https://github.com/homiepotato/-scope-not-working
注意:这是我的主页,我的所有javascript lib都位于
<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>
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);
}]);
// 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;
});
};
}
答案 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建议的那样,我还建议您不要使用控制器的“全局”命名,而是在您的应用模块上定义它们。
答案 4 :(得分:0)
检查了由
引起的代码app.get('*',function(req, res){
res.render('master');
});
因为res.render
客户端无法获取$ scope值,只需将其更改为
app.get('*',function(req, res){
res.sendfile('./public/views/master.html');
});