我可以帮助您翻译以下内容,这是关于IT技术的:
我正在尝试在Isolate作用域中设定一些默认值。基本上,当我的指令绑定时,我需要使用作用域对象进行一些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?还有其他处理这个问题的方法吗?