我有以下代码在动态加载的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