我正在一个在其他地方调用的函数内初始化控制器和指令。因此可以将函数的param值发送到controller / directive。这是我的代码。
var myModule = angular.module('myModule', []);
var init = function initialize(id) {
myModule.controller('myController', ['$scope', function ($scope) {
// Here I want to use parameter value id
}]);
myModule.directive('myDirective', function () {
var Directive = {};
Directive.template = 'Hello World!';
Directive.link = function (scope, element, attrs) {
// Here I want to use parameter value id
}
return Directive;
});
$('body').attr('ng-controller', 'myController');
angular.bootstrap($('body'), ['myModule']);
}
在这里,我能够从外部成功调用init,但是我无法在控制器或指令中获取id的值。
答案 0 :(得分:0)
您构建代码的方式,您不必明确传递ID。控制器在一个可以访问id的闭包内定义。这意味着您可以使用id,就像它作为参数传递给控制器或指令一样。
我建议您阅读本文了解详情:http://www.w3schools.com/js/js_function_closures.asp