I'm currently trying out how to implement angular in OOP. My testsite looks like this:
class userData {
catchDropDownSelection(){
this.dropDownSelection = $('#xDropDown option:selected').text();
console.log("dropDownSelection is ", this.dropDownSelection)
}
}
class sendData{
constructor(userData){
this.userDataClone = userData;
}
sendDropDownSelection(){
//this.userDataClone.catchDropDownSelection();
console.log("this.userDataClone.dropDownSelection inside sendDropDownSelection is ", this.userDataClone.dropDownSelection)
$.post("MyFirstPhP.php", {
userSelection : this.userDataClone.dropDownSelection
}, function (data){
//this.testFunction()
$('#testOutput').html(data);
//document.getElementById("testOutput").innerHTML = "data"
}).fail(function(){
console.log("$.post failed!");
})
}
testFunction(){
//document.getElementById("testOutput").innerHTML = "data"
$('#testOutput').html("data");
}
}
class angularApps {
constructor(sendData, userData){
this.sendDataClone = sendData;
this.userDataClone = userData;
}
xMyApp(){
this.myApp = angular.module('xMyApp', []);
this.myApp.controller("xMyCtrl", function($scope){
$scope.data1 = "test1";
$scope.data2 = "test2";
})
}
}
class setEvents {
constructor(sendData, userData){
this.sendDataClone = sendData;
this.userDataClone = userData;
}
onClickEvents(){
$('#sendDataButton').click(function(){
this.userDataClone.catchDropDownSelection()
//console.log("index catched is ", this.sendDataClone.userDataClone.dropDownSelection)
}.bind(this))
$('#sendDataButton').click(function(){
this.sendDataClone.sendDropDownSelection()
}.bind(this))
}
addAllEvents(){
this.onClickEvents()
}
}
var userDataInstance = new userData;
var sendDataInstance = new sendData(userDataInstance);
var angularAppsInstance = new angularApps(sendDataInstance, userDataInstance);
var setEventsInstance = new setEvents(sendDataInstance, userDataInstance);
/*y
var myApp = angular.module('xMyApp', []);
myApp.controller("xMyCtrl", function($scope){
$scope.data1 = "test1";
$scope.data2 = "test2";
})*/
<!DOCTYPE html>
<html>
<head>
<title>PHP is Awesome!</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<link rel = "stylesheet" type = "text/css" href = "MyFirstPhP.css">
</head>
<body onload="setEventsInstance.addAllEvents(); angularAppsInstance.xMyApp()">
<div ng-app="xMyApp" ng-controller="xMyCtrl">
{{data1 + " " + data2}}
</div>
<div id = "fancyBackGround">
<select id = "xDropDown">
<option value = "test1">test1</option>
<option value = "test2">test2</option>
</select>
<button id="sendDataButton">ClickMe!</button>
<p id = "testOutput">not yet</p>
</div>
<script src="MyFirstPhP.js"></script>
</body>
</html>
<?php
session_start();
$dropDownSelection = $_POST['userSelection'];
echo $dropDownSelection
//$dropDownSelection = $dropDownSelection . "wurde verarbeitet!";
?>
The code sections which are the most important are those:
class angularApps {
constructor(sendData, userData){
this.sendDataClone = sendData;
this.userDataClone = userData;
}
xMyApp(){
this.myApp = angular.module('xMyApp', []);
this.myApp.controller("xMyCtrl", function($scope){
$scope.data1 = "test1";
$scope.data2 = "test2";
})
}
}
and
<div ng-app="xMyApp" ng-controller="xMyCtrl">
{{data1 + " " + data2}}
</div>
So, when I'm running the site, using OOP the div associated with angular first displays the whole angular expression string {{data1 + " " + data2}} and then immediately switches to the resolved results after the page has completely loaded (see the onload functions inside the bodytag).
When I'm not using OOP, this cosmetical issue vanishes. First I thought I could solve this issue by simply calling the xMyApp() method from inside the script, which looks like this:
[full scriptcode]
angularAppsInstance.xMyApp()
I thought this would help because it would be similar to non-OOP, where any functionality not specifically excluded from onloadexecution is executed ASAP. But actually, it doesn't change anything.
So, is there any elegant way to solve this issue without dropping the OOP-approach entirely? Or is it a "bad" idea to use angular inside OOP code anyway? I've never used angular before, so I would be glad if someone could give me a tip.
EDIT: If you want to try out yourself how the OOP/non-OOP approach differ, simply comment out:
var angularAppsInstance = new angularApps(sendDataInstance, userDataInstance);
and
angularAppsInstance.xMyApp()
and comment in this part:
var myApp = angular.module('xMyApp', []);
myApp.controller("xMyCtrl", function($scope){
$scope.data1 = "test1";
$scope.data2 = "test2";
})
{ }icon in the editor toolbarOOPas if it would have to do anything with your problem? You probably just did something right before that you do wrong now. Can you show the code where it worked?