2015-07-10 119 views
0

有人可以帮助我,我不知道我做错了什么。我遵循平均网络开发教程。 Ng-include可以与模板一起工作,但不能使用ng-view。我有5个文件。首先是Users.client.module.js:ng view is not working but ng include working in angularjs

angular.module('users', []); 

二是users.client.controller.js

angular.module('users',['ngRoute']).controller('UsersController', ['$scope', 
function ($scope){ 
    $scope.name = 'UsersCont'; 
    console.log("from UsersCont"); 
}]); 

三是users.client.routes.js

angular.module('users').config(['$routeProvider', function ($routeProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl: 'users/views/register-user.client.view.html' 

    }) 
    .otherwise({ 
     templateUrl: '/somewhere else' 
    }); 
}]); 

四is register-client-view.html

<section ng-controller="UsersController"> 
    <h1>{{ name }}</h1> 
</section> 

and last is ind ex.html

<section ng-include=" 'users/views/register-user.client.view.html' "></section> 

<section ng-view> </section> 

<script type="text/javascript" src="https://stackoverflow.com/users/users.client.module.js"></script> 
    <script type="text/javascript" src="https://stackoverflow.com/users/controllers/users.client.controller.js"></script> 

请帮

+0

我在您的index.html中的任何地方都看不到'ng-app'这是引导ng应用程序所必需的。这似乎很奇怪'ng-include =“'users/views/register-user.client.view.html'”' - 为什么你在里面有额外的引号? –

+0

我有手动引导文件application.js var mainApplicationModuleName ='myApp'; var mainApplicationModule = angular.module(mainApplicationModuleName,['ngResource','ngRoute','users','articles']); mainApplicationModule.config(['$ locationProvider',function($ locationProvider){ \t $ locationProvider.hashPrefix('!'); }]); (document,[mainApplicationModuleName]); }); ng-include工作正常,我试过没有单引号,但没有工作。我认为问题是与路线的某处,但我无法找到它。也尝试与ng-app – userDC

+1

我没有看到你加载'users.client.routes.js'在你的索引。你有这个吗? –

回答

1

你必须使用ngRoute作为一个依赖该

试试下面的代码

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

而且也不要忘了包括角路线js文件在角度主要的js文件之后。

<script src="Scripts/Vendors/angular-route.min.js"></script> 
+0

感谢您的快速回复Hasib Hasan Arnab,我试过之前,我问了['ngRoute']问题在我的module.js文件中,但也没有工作。我已经在angular.js之后包含了angular-route.js,现在我尝试了缩小版本,但仍然无法工作......真的不知道该怎么做......再一次感谢你帮助我 – userDC

1

似乎问题是由模板名称中的连字符引起的。 看到这个答案AngularJS templates can't use JSON that contains hyphen

尝试重命名模板名称(用下划线替换连字符)或用反斜杠转义它。

我想连字符被认为是减号和角(或javascript),然后被它混淆。这也是它工作的原因ng-include=" 'users/views/register-user.client.view.html' ",这是行不通的ng-include="users/views/register-user.client.view.html"

+0

谢谢你的帮助,这就是为什么是单引号。但我认为这不是问题。我也在文章路由文章/ views/list-articles.client.view.html和工作正常时,路由是'/ articles'...我认为问题是路由任何模板,我把ng-view不显示。我会重新开始,并尝试找到错误...现在我不知道... – userDC