【发布时间】:2017-10-27 20:26:29
【问题描述】:
我不确定这个术语是什么,但我需要为我的应用程序中的所有组件提供相同的数据。这是我的应用程序的基本结构:
BeforeAngular.js
function DataModel(json){
this.name = json.name || "No name"
}
//// JQuery ////
function getDataModel(name) {
return $.get(baseURL + "data-model/" + name).then(response => {
return new DataModel(response)
})
}
AppSetup.js
var app = angular.module('MyApp', ['ui.router', 'ngResource', ...]);
app.config(function($stateProvider,$httpProvider,$urlRouterProvider,$sceProvider) {
$sceProvider.enabled(false)
// Each of these components need `getDataModel()` - how can I do that once and send it to all of them with Angular
states = [
{
name: 'Interactor',
url: '/interactor',
component: 'interactorcomponent'
},
{
name: "otherwise",
url: "*path",
component: "viewMDLcomponent"
}
];
states.forEach(state => $stateProvider.state(state))
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
ViewMDLComp.js
angular.module("MyApp").component("viewMDLcomponent", {
templateUrl: "html/View_MDL.html",
controllerAs: "c",
controller: [..., View_MDL_Controller]
})
function View_MDL_Controller($http,$timeout,$filter,$log,$q,$sce,l,FileSaver,Blob) {
var c = this
$q.when(getAllDataModels().then(r => {
console.log("getAllDataModels");
c.allMisDataModels = r
})
}
那么,我如何在 Angular 中运行一些在我的组件之前编译的东西,并为它们提供所需的数据?
【问题讨论】:
-
创建一个服务并在需要的地方注入它
-
@OlegMarkoff 那会是什么样子?您介意用示例发布答案吗?
标签: angularjs