[英]Angular Service not being called
我正在使用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-app指令位於_Layout.cshtml文件中。
<html data-ng-app="myApp">
專輯是一個數組。 選中{{相冊[0]。名稱}}。
使用app.factory而不是app.service
所以
app.factory('albumService', function(){
return albuns;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.