我想在隔離範圍的指令中加入一些默認值。基本上,當我的指令被綁定時,我需要使用scope對象做一些DOM操作。下面是我的代碼:AngularJS:指令無法訪問隔離範圍對象
控制器:
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" />
現在,問題是,在試圖訪問內部指令的默認方法的分離範圍,我AAM得到一個未定義的值,而數據即將到來,越來越被綁定到DOM。如何訪問廣播監聽器中的隔離範圍並修改指令模板HTML?處理這個問題還有另一個問題嗎?
你能創建一個小提琴並不完全到達? – AlwaysALearner