2013-12-23 116 views
0

我使用AngularJS和MVC 5的版本1.2.2,我有从获取执行让我的服务代码的问题。角服务不会被调用

这里是我在我的_Layout.cshtml文件链接

div class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav"> 
        <li>@Html.ActionLink("Home", "Index", "Home", null, new { @class = "navbar-brand" })</li> 
        <li>@Html.ActionLink("Albums", "Index", "Album")</li> 
</ul> 
</div> 

我的模块看起来像这样

var app = angular.module('myApp', ['ngRoute']); 
app.config(function ($routeProvider) { 
    $routeProvider 
     .when('/Home', 
      { 
       controller: 'HomeController', 
       templateUrl: '/Views/Home/Index.cshtml' 
      }) 
     .when('/Album', 
      { 
       controller: 'AlbumController', 
       templateUrl: '/View/Album/Index.cshtml' 
      }) 
     .otherwise({ redirectTo: '/Home' }); 
}); 

AlbumController看起来像这样

app.controller('AlbumController', function ($scope, albumService) { 

    init(); 

    function init() { 
     $scope.albums = albumService.getAlbums(); 
    } 
}); 

albumService看起来是这样的

app.service('albumService', function() { 
    this.getAlbums = function() { 
     return albums; 
    }; 

    var albums = [ 
     { 
      id: 1, Name: 'Foo', AlbumNumber: 1, 
      Songs: [ 
       { Id: 1, Name: 'Bar', AlbumId: 1, TrackNumber: 1 } 
      ] 
     } 
    ]; 
}); 

View/Album/Index.cshtml看起来是这样的:

{{album.Name}}

的问题是,专辑名不显示在页面上。我究竟做错了什么?

编辑

我的NG-程序指令是在我_Layout.cshtml文件。

<html data-ng-app="myApp"> 
+0

哪里是你的NG-应用程序的指令? – billb

+0

即将更新文本。 –

+0

如果您查看源文件,您是否看到包含角文字?由于上面的代码片段中没有它们,因此尚不清楚。 – billb

回答

0

专辑是一个数组。检查{{albums [0] .Name}}。

+0

同样,没有打印。 –

+0

控制台中的任何错误? – Alborz

+0

nope - 没有错误 –

-1

使用app.factory而不是app.service

所以

app.factory('albumService', function(){ 

    return albuns; 
}); 
+2

解释你的答案,不要用'Thanks'或任何东西来签名。谢谢。 – Stewie