无法在 AngularJS 中设置服务变量
Trouble setting a service variable in AngularJS
这是我在 AngularJS 中的第一个项目,这是我一直想尝试一段时间的东西。我正在使用 Foundation Apps Framework(基于 Angular),但我在设置服务变量时遇到了一些问题。
这个想法是让服务变量存储应用程序播放器(使用 Videogular 插件)的 "now playing" 曲目列表,我想这与购物篮的方式类似。
我 获取 "now playing" 曲目列表没问题,但我似乎无法 设置带有测试数据的曲目列表(使用带有 ng-click="setTracklist()
的简单按钮进行测试)
如果有任何建议,我将不胜感激。
如果这看起来是一个非常简单的问题,我深表歉意,这对我来说是一个很大的学习曲线。
服务 (app.js)
(function() {
'use strict';
angular.module
.service('nowPlaying', function($sce) {
var tracklist = [{"info": [{"title": "Lies (Tourist Remix)","artist": "CHRVCHES","artwork": "https://i1.sndcdn.com/artworks-000062986758-w3vnj1-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/kislacansu/chvrches-lies-tourist-remix","order": "0"}],"sources":[{"src":$sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/120555166/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type":"audio/mpeg"}]},{"info": [{"title": "Midnight (Jon Hopkins Remix)","artist": "Coldplay","artwork": "http://geo-media.beatport.com/image_size/500x500/9453088.jpg","service": "Dropbox","url": "http://dropbox.com","order": "1"}],"sources": [{"src": $sce.trustAsResourceUrl('https://www.dropbox.com/s/hmmrarbb6jry2id/04%20Midnight%20%28Jon%20Hopkins%20Remix%29.mp3?dl=1'),"type": "audio/mpeg"}]},{"info": [{"title": "Lovers in Japan","artist": "Coldplay","artwork": null,"service": "YouTube","url": "https://www.youtube.com/watch?v=G7miUwfuWLU","order": "2"}],"sources": [{"src": "https://www.youtube.com/watch?v=0c4tjuw3NWg","type": "video/mp4"}]},{"info": [{"title": "All We'll Know","artist": "The Hics","artwork": "https://i1.sndcdn.com/artworks-000078234891-3ua2os-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/the-hics/all-well-know","order": "3"}],"sources": [{"src": $sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/147550599/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type": "audio/mpeg"}]}];
this.setTracklist = function(){
tracklist = [{"info": [{"title": "All We'll Know","artist": "The Hics","artwork": "https://i1.sndcdn.com/artworks-000078234891-3ua2os-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/the-hics/all-well-know","order": "3"}],"sources": [{"src": $sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/147550599/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type": "audio/mpeg"}]}];
console.log("something else");
console.log(tracklist);
return tracklist;
}
this.getTracklist = function() {
return tracklist;
};
})
;
})();
控制器 (app.js)
angular.module('application')
.controller('indexCtrl', indexCtrl)
;
indexCtrl.$inject = ['$scope', '$stateParams', '$state', '$controller', '$http', '$sce', '$timeout', 'nowPlaying'];
function indexCtrl($scope, $stateParams, $state, $controller, $http, $sce, $timeout, nowPlaying) {
//Other Videogular code has been edited out...
//Controller videos gets service tracklist
controller.videos = nowPlaying.getTracklist();
controller.config = {
preload: "auto",
autoHide: false,
autoHideTime: 3000,
autoPlay: true,
sources: controller.videos[0].sources,
load: false,
transclude: true,
controls: undefined,
theme: {
url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
},
plugins: {
poster: controller.posters
}
};
controller.setVideo = function(index) {
controller.API.stop();
controller.currentVideo = index;
controller.config.sources = controller.videos[index].sources;
$timeout(controller.API.play.bind(controller.API), 100);
};
}
不要使用 this
关键字 因为函数上下文会更改 this 值。做这样的事情:
var _this = this;
_this.setTracklist = function(){}
此外,您不能使用 ng-click
从 html 页面调用 service
函数,您需要在控制器中调用函数。
controller.setTracklist = function() {
nowPlaying.setTracklist();
}
这是我在 AngularJS 中的第一个项目,这是我一直想尝试一段时间的东西。我正在使用 Foundation Apps Framework(基于 Angular),但我在设置服务变量时遇到了一些问题。
这个想法是让服务变量存储应用程序播放器(使用 Videogular 插件)的 "now playing" 曲目列表,我想这与购物篮的方式类似。
我 获取 "now playing" 曲目列表没问题,但我似乎无法 设置带有测试数据的曲目列表(使用带有 ng-click="setTracklist()
的简单按钮进行测试)
如果有任何建议,我将不胜感激。 如果这看起来是一个非常简单的问题,我深表歉意,这对我来说是一个很大的学习曲线。
服务 (app.js)
(function() {
'use strict';
angular.module
.service('nowPlaying', function($sce) {
var tracklist = [{"info": [{"title": "Lies (Tourist Remix)","artist": "CHRVCHES","artwork": "https://i1.sndcdn.com/artworks-000062986758-w3vnj1-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/kislacansu/chvrches-lies-tourist-remix","order": "0"}],"sources":[{"src":$sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/120555166/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type":"audio/mpeg"}]},{"info": [{"title": "Midnight (Jon Hopkins Remix)","artist": "Coldplay","artwork": "http://geo-media.beatport.com/image_size/500x500/9453088.jpg","service": "Dropbox","url": "http://dropbox.com","order": "1"}],"sources": [{"src": $sce.trustAsResourceUrl('https://www.dropbox.com/s/hmmrarbb6jry2id/04%20Midnight%20%28Jon%20Hopkins%20Remix%29.mp3?dl=1'),"type": "audio/mpeg"}]},{"info": [{"title": "Lovers in Japan","artist": "Coldplay","artwork": null,"service": "YouTube","url": "https://www.youtube.com/watch?v=G7miUwfuWLU","order": "2"}],"sources": [{"src": "https://www.youtube.com/watch?v=0c4tjuw3NWg","type": "video/mp4"}]},{"info": [{"title": "All We'll Know","artist": "The Hics","artwork": "https://i1.sndcdn.com/artworks-000078234891-3ua2os-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/the-hics/all-well-know","order": "3"}],"sources": [{"src": $sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/147550599/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type": "audio/mpeg"}]}];
this.setTracklist = function(){
tracklist = [{"info": [{"title": "All We'll Know","artist": "The Hics","artwork": "https://i1.sndcdn.com/artworks-000078234891-3ua2os-t500x500.jpg","service": "SoundCloud","url": "https://soundcloud.com/the-hics/all-well-know","order": "3"}],"sources": [{"src": $sce.trustAsResourceUrl('http://api.soundcloud.com/tracks/147550599/stream?client_id=b49f9732e4efc7dc0e497012d17b2695'),"type": "audio/mpeg"}]}];
console.log("something else");
console.log(tracklist);
return tracklist;
}
this.getTracklist = function() {
return tracklist;
};
})
;
})();
控制器 (app.js)
angular.module('application')
.controller('indexCtrl', indexCtrl)
;
indexCtrl.$inject = ['$scope', '$stateParams', '$state', '$controller', '$http', '$sce', '$timeout', 'nowPlaying'];
function indexCtrl($scope, $stateParams, $state, $controller, $http, $sce, $timeout, nowPlaying) {
//Other Videogular code has been edited out...
//Controller videos gets service tracklist
controller.videos = nowPlaying.getTracklist();
controller.config = {
preload: "auto",
autoHide: false,
autoHideTime: 3000,
autoPlay: true,
sources: controller.videos[0].sources,
load: false,
transclude: true,
controls: undefined,
theme: {
url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
},
plugins: {
poster: controller.posters
}
};
controller.setVideo = function(index) {
controller.API.stop();
controller.currentVideo = index;
controller.config.sources = controller.videos[index].sources;
$timeout(controller.API.play.bind(controller.API), 100);
};
}
不要使用 this
关键字 因为函数上下文会更改 this 值。做这样的事情:
var _this = this;
_this.setTracklist = function(){}
此外,您不能使用 ng-click
从 html 页面调用 service
函数,您需要在控制器中调用函数。
controller.setTracklist = function() {
nowPlaying.setTracklist();
}