我有自定义指令,比如:
angular.module('app.directive').directive('myProductSwitcherDropdown',myProductSwitcherDropdown);
myProductSwitcherDropdown.$inject = ['$compile'];
function myProductSwitcherDropdown() {
return {
restrict: 'E',
transclude: 'true',
scope: {
domainobject:"=",
ctrlFn:"&"
},
templateUrl: "src/directive/templates/my-product-switcher-dropdown.html",
controller: ['$scope', 'UtilService', function($scope,UtilService) {
debugger;
var self = this;
$scope.instance =self;
self.dropDownChanged = function(item) {
debugger;
$scope.ctrlFn();
};
}],
controllerAs: 'myProductSwitcherDrpdwnCtrl'
}
}我引用的指令如下:
<div class='notification-tray'></div>
<div class="left-menu pull-left">
<my-product-switcher-dropdown domainobject="domainobject" ctrl-fn="ctrlFn()">
</my-product-switcher-dropdown>
</div>但是在我的控制器中,当我在ctrlFn()内部使用say $location or $window时,它将以未定义的的形式出现。
,我也注射过了。
angular.module('workspaces.domain').controller('DomainController',DomainController);
DomainController.$inject = ['$scope', '$rootScope', '$location'];
function DomainController ($scope, $rootScope, $location) {
var self = this;
....
....
//$location is accessible here though
$scope.ctrlFn = function () {
//Undefined here
debugger;
};在ctrlFn()之前,$location是可访问的,但在内部是不可访问的。我做错什么了?
发布于 2019-01-05 15:18:26
这是开发人员控制台的工件。内部代码需要引用$location,以便JavaScript引擎创建闭锁
angular.module('workspaces.domain').controller('DomainController',DomainController);
DomainController.$inject = ['$scope', '$rootScope', '$location'];
function DomainController ($scope, $rootScope, $location) {
var self = this;
....
....
//$location is accessible here though
$scope.ctrlFn = function () {
//ADD reference to $location
$location;
//OR
console.log($location);
//Will also be defined here
debugger;
};这两种方法都迫使JavaScript引擎创建一个对开发人员控制台调试器可见的闭锁。
https://stackoverflow.com/questions/54036617
复制相似问题