Saturday, 12 August 2017

angularjs - Add a "parent" function to run before all components

Answer



I'm not sure what the term is, but I need to give the same data to all components in my app. Here's the basic structure of my app:



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
})
}


So how Can I run something in Angular that compiles before my components, and feeds them the data they need?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...