我试图在我的指令中使用隔离范围放置一些默认值。基本上,当我的指令被绑定时,我需要使用范围对象进行一些 DOM 操作。下面是我的代码:
控制器:
angular.module('ctrl').controller('TempCtrl', function($scope, $location, $window, $timeout, RestService, CommonSerivce) {
$scope.showAppEditWindow = function() {
//Binding the directive isolate scope objects with parent scope objects
$scope.asAppObj = $scope.appObj;
$scope.asAppSubs = $scope.appSubscriptions;
//Making Initial Settings
CommonSerivce.broadcastFunction('doDirectiveBroadcast', "");
};
服务:
angular.module('Services').factory('CommonSerivce', function ($rootScope) {
return {
broadcastFunction: function(listener, args) {
$rootScope.$broadcast(listener, args);
}
};
指示:
angular.module('directives').directive('tempDirective', function() {
return {
restrict : 'E',
scope:{
appObj:'=asAppObj',
appSubs: '=asAppSubs'
},
link : function(scope, element, attrs) {},
controller : function ($scope,Services,CommonSerivce) {
//Broadcast Listener
$scope.$on('doDirectiveBroadcast', function (event, args) {
$scope.setDefaults();
});
$scope.setDefaults = function() {
//Setting Default Value
alert(JSON.stringify($scope.appSubs)); //Coming as undefined
};
},
templateUrl:"../template.html"
};
});
自定义指令元素:
<temp-directive as-app-obj="asAppObj" as-app-subs="asAppSubs" />
现在,问题是,在尝试访问指令内部默认方法中的隔离范围时,我得到一个未定义的值,而数据即将到来并绑定到 DOM。如何访问广播侦听器中的隔离范围并修改指令模板 HTML?是否有另一种方法来处理这个问题?