Tôi đang cố gắng đặt một số giá trị mặc định trong chỉ thị của mình với phạm vi Cô lập. Về cơ bản, tôi cần phải thực hiện một số thao tác DOM bằng cách sử dụng đối tượng phạm vi khi chỉ thị của tôi bị ràng buộc. Dưới đây là mã của tôi:AngularJS: Chỉ thị không thể truy cập đối tượng phạm vi cô lập
Bộ điều khiển:
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', "");
};
dịch vụ:
angular.module('Services').factory('CommonSerivce', function ($rootScope) {
return {
broadcastFunction: function(listener, args) {
$rootScope.$broadcast(listener, args);
}
};
Chỉ thị:
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"
};
});
Tuỳ chỉnh Chỉ thị yếu tố:
<temp-directive as-app-obj="asAppObj" as-app-subs="asAppSubs" />
Bây giờ, vấn đề là khi cố gắng truy cập vào phạm vi cô lập trong phương thức mặc định bên trong chỉ thị, thì tôi nhận được một giá trị không xác định trong khi dữ liệu đang đến và đang bị ràng buộc với DOM. Làm thế nào tôi có thể truy cập phạm vi cách ly trong trình nghe phát sóng và sửa đổi HTML mẫu chỉ thị? Có cách nào khác để xử lý điều này?
Bạn có thể tạo một câu đố không? – AlwaysALearner