I have a login page where I enter the credentials. When correct credentials are sent to the server, the server responses with string that contain new elements including scripts. The element set returned by the server then replaces the body
content of the login page. The problem is that when the new content is loaded into the document angular functions don't work.
This is the login page:
<!DOCTYPE html>
<html>
<head>
</head>
<body ng-app="loginApp">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript" src="js/login/loginMain.js">
</script>
Login<br><br>
<form ng-controller="loginController" ng-submit="postForm()">
User Name:<br>
<input ng-model="unameInputField" type="text" name="user[name]"><br>
Password:<br>
<input ng-model="passwordInputField" type="password" name="user[password]">
<input type="submit" name="Submit">
</form>
</body>
</html>
and this is loginMain.js
:
/* public/js/controllers/apps *************************************/
angular.module("loginApp", []);
angular.module("loginApp").controller("loginController", function($scope, $http){
$scope.unameInputField = "";
$scope.passwordInputField = "";
$scope.postForm = function() {
$http.post("/credentials.json", {username: $scope.unameInputField, password: $scope.passwordInputField},
{headers: {'Content-Type': 'application/json'}}).then(
function(res) { //Success
// window.location.href = 'http://localhost:3000/home';
console.log(res.data)
$("body").attr("ng-app", "myApp");
$("body").html(res.data);
},
function(res) { // Failed
alert("POST FAILED, Server is most probably offline")
}
)
}
})
This is the html elements I want to place inside the body
element:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<title>Home Electricity Manager </title>
<h1 id="the-header">Wellcome to home electricity manager radoslav.k.marinov@gmail.com!</h1>
<div id="button-1-div" ng-controller="myController" order="1" style="text-align: center; display: inline-block;" ng-init="spanText=S4555">
<span id="button-1-span-1" ng-click="changeText()" style="white-space:pre;">{{spanText}}</span><br>
<button id="butt-1" ng-style="myStyle" ng-click="toggleRelay()">{{ButtonStatus}}</button>
</div><br>
<div id="button-2-div" ng-controller="myController" style="text-align: center; display: inline-block;">
<span id="button-2-span-2" ng-click="changeText()" style="white-space:pre;">{{spanText}}</span><br>
<button id="butt-2" ng-style="myStyle" ng-click="toggleRelay()">{{ButtonStatus}}</button>
</div><br>
<div change-cred></div>
I see in the developers view of the Chrome Browser that the content is actually replaced but the user view looks like angular doesn't compile the new content. This is the picture of the page:
While if the angular functions are working properly the user page would look like this:
Excuse my bad English! I hope you get the idea and what is the problem indeed!