我有以下代碼在動態加載的HTML使用角度控制器
app.js
var app = angular.module('myModule', ['schemaForm', 'base64', 'ngRoute']);
taskController.js
app.controller('TaskController', function($scope, AuthService, $http, $base64, $location) {
$scope.fetchFormKey = function() {
$http.defaults.headers.common['Authorization'] = 'Basic ' + AuthService.auth;
$http.get(restServerURL + '/task/' + $scope.taskId + '/form').
success(function(data) {
$scope.formKey = "./partials/itpTrainingCreation/" + data.key + ".html";
});
}
}
task.html
<h2>Generated form</h2>
<div ng-controller="TaskController" ng-init="fetchFormKey()">
<div ng-include src="formKey"></div>
</div>
在動態加載文件(loade d by ng-include)我想定義這個文件將要使用的角度控制器。
dynamic.html(例如)
<script>
angular.module('myModule').controller('DymanicController', function($scope, AuthService, $http, $base64, $location) {
$scope.exampleValue="myCustomValue";
});
</script>
<div ng-controller="DymanicController">
{{exampleValue}}
</div>
不幸的是我得到以下錯誤: http://errors.angularjs.org/1.5.5/ng/areq?p0=DymanicController&p1=not%20a%20function%2C%20got%20undefined
我怎樣才能改變我的代碼來解決這個問題?
http://stackoverflow.com/questions/15250644/loading-an-angularjs-controller-dynamically – Thalaivar