我与AngularJs和PHP的工作,我面临的问题时,我不得不从加载其他页面,然后AngularJs停止工作内容。
下面是一些示例代码,以显示我的方案。
主page.php文件
<div id="form-section">
<button id="load_form">Load Form</button>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript">
$(document).on("click", "#load_form", function() {
$.ajax({
methond: 'GET',
url: 'load-form.php',
success: function(resp) {
$("#form-section").html(resp);
}
});
});
</script>
负载form.php的
<form ng-app="myApp" ng-controller="myCtrl">
{{ textOne }}
<input type="text" ng-model="textOne">
</form>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.textOne = "John";
});
</script>
如果我在<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
把load-form.php
并直接在浏览器中加载它,然后AngularJs那里工作的罚款。但是,当我在主网页加载通过Ajax然后我AngularJs代码停止工作
主page.php文件
<div id="form-section">
<button id="load_form">Load Form</button>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript">
var setInnerHtml = function(elm, html) {
elm.innerHTML = html;
var scripts = elm.getElementsByTagName("script");
// If we don't clone the results then "scripts"
// will actually update live as we insert the new
// tags, and we'll get caught in an endless loop
var scriptsClone = [];
for (var i = 0; i < scripts.length; i++) {
scriptsClone.push(scripts[i]);
}
for (var i = 0; i < scriptsClone.length; i++) {
var currentScript = scriptsClone[i];
var s = document.createElement("script");
// Copy all the attributes from the original script
for (var j = 0; j < currentScript.attributes.length; j++) {
var a = currentScript.attributes[j];
s.setAttribute(a.name, a.value);
}
s.appendChild(document.createTextNode(currentScript.innerHTML));
currentScript.parentNode.replaceChild(s, currentScript);
}
}
$(document).on("click", "#load_form", function() {
$.ajax({
methond: 'GET',
url: 'load-form.php',
success: function(resp) {
setInnerHtml($("#form-section")[0], resp);
}
});
});
</script>
负载form.php的
<form ng-app="myApp" ng-controller="myCtrl" id="myApp">
{{ textOne }}
<input type="text" ng-model="textOne">
</form>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.textOne = "John";
});
setTimeout(function() {
angular.bootstrap(document.getElementById('myApp'), ['myApp'], 100);
// Closing }); was missing here
});
</script>