AngularJS Live Example
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div ng-controller="InitCtrl" >
<div compile-data template="{{mainPage}}"></div>
</div>
JavaScript
var fessmodule = angular.module('myModule', []);
fessmodule.controller('InitCtrl', function ($http,$scope, tempService) {
$scope.mainPage = tempService();
$scope.login = function () {
alert("in login");
$http.post("login", {password: $scope.password, username: $scope.username}).
success(function (data, status, headers, config) {
$scope.mainPage = data;
console.log("successfully logged to login");
}).error(function (data, status, headers, config) {
console.log("error in post");
});
};
});
fessmodule.$inject = ['$http', '$scope','myTplService'];
fessmodule.directive( 'compileData', function ( $compile ) {
return {
scope: true,
link: function ( scope, element, attrs ) {
var elmnt;
attrs.$observe( 'template', function ( myTemplate ) {
if ( angular.isDefined( myTemplate ) ) {
// compile the provided template against the current scope
elmnt = $compile( myTemplate )( scope );
element.html(""); // dummy "clear"
element.append( elmnt );
}
});
}
};
});
fessmodule.factory( 'tempService', function () {
return function () {
// $http.post("login", {password: $scope.password, username: $scope.username}).
// success(function (data, status, headers, config) {
//
// console.log("successfully logged to login");
// return data;
// }).error(function (data, status, headers, config) {
// console.log("error in post");
// return "error";
// });
// obviously would be $http
return '<form class= "form-signin" >' +
'<h2 class="form-signin-heading">Please sign in</h2>' +
'<input type = "text" class= "input-block-level" placeholder = "User name" ng-model = "username" >' +
'<input type="password" class="input-block-level" placeholder="Password"...