具有隔离范围的自定义指令功能绑定不起作用,我不知道为什么?
我有dropdown
指令,该指令可以按预期工作,并且在更改值时会调用dropValueChanged
函数。
在我的指令中,我正在使用以下代码:
angular.module('app.directive').directive('myExtraDropdown',myExtraDropdown);
myExtraDropdown.$inject = ['$compile'];
function myExtraDropdown() {
return {
restrict: 'E',
transclude: 'true',
scope: {
ctrlFn:"&"
},
templateUrl: "src/templates/my-extra-dropdown.html",
link: function(scope) {
},
controller: ['$scope', 'UtilService', function($scope,UtilService) {
var self = this;
self.dropValueChanged = function(item) {
self.ctrlFn({id:item.value});
};
}],
controllerAs: 'myExtraDrpdwnCtrl'
}
}
我正在使用类似的指令:
<div class="left-menu pull-left">
<my-extra-dropdown ctrl-fn="myCtrl.ctrlFn(id)">
</my-extra-dropdown>
</div>
然后,在我的ctrl
中,我定义了函数,但未调用该函数。
angular.module('my.workspaces').controller('myController',myController);
myController.$inject = ['$scope', '$rootScope','$location'];
function myController($scope, $rootScope, $location) {
var self = this;
self.ctrlFn = function(id) {
let oldUrl = $location.href;
}
我的函数没有被调用。
详细信息
但是如果我们更改我的directive
来打来电话
$scope.ctrlFn({id:item.value});
,如果不是,则在我的控制器中,而不是self
$scope.ctrlFn
然后被称为。 但是$ location不能在其中访问。
我在做什么错了?