2017-03-02 218 views
1

我有一個指令和一個控制器。該指令的休耕:Angular指令和控制器

calcP.directive('modalDialog', function() { 
    return { 
     restrict: 'E', 
     scope: { 
      show: '=' 
     }, 
     replace: true, 
     transclude: true, 
     link: function(scope, element, attrs) { 
      scope.dialogStyle = {}; 
      if (attrs.width) 
       scope.dialogStyle.width = attrs.width; 
      if (attrs.height) 
       scope.dialogStyle.height = attrs.height; 
      **scope.hideModal = function() { 
       scope.show = false; 
       delete $sope.types.individual;** 
      }; 
     }, 
     template: "..." 
    }; 
}); 

我的控制器:

calcP.controller('calcPCtrl', function($scope, $http, $window, emailSenderEndpoint) { 

$scope.getVariantDomovoy = function() { 
     $scope.types.domovoy = $scope.variants.domovoy; 
    }; 
    $scope.getVariantIndividual = function() { 
     $scope.types.individual = $scope.variants.individual; 
    }; 

    ... 
    $scope.modalShown = false; 
     $scope.toggleModal = function() { 
      $scope.modalShown = !$scope.modalShown; 
     }; 

    }); 

我的模板:

template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>" 

我想將它添加到一個函數來刪除一些$範圍。但瀏覽器顯示錯誤,它無法找到變量$ scope.types.individual。

我只是試着自己學習AngularJS,還是有一些問題。

+0

'$刪除sope.types.individual;'?或'刪除$ scope.types.individual;'?? – DilumN

+0

正如我正確理解你的'類型'是在你的控制器上?所以你可以'刪除$ scope。$ parent.types.individual;'但是你似乎正在嘗試實現某些東西並且使用非角度的方式來做到這一點 – devqon

+0

@DumumN是的,$ scope,抱歉..但是我仍然有相同的錯誤 –

回答

2

如果要從指令中更改控制器的值,首先必須使用雙向綁定將該變量傳遞給指令。然後你可以改變這個值如下。

calcP.directive('modalDialog', function() { 
    return { 
     restrict: 'E', 
     scope: { 
      show: '=', 
      types: '=' 
     }, 
     replace: true, 
     transclude: true, 
     link: function(scope, element, attrs) { 
      scope.dialogStyle = {}; 
      if (attrs.width) 
       scope.dialogStyle.width = attrs.width; 
      if (attrs.height) 
       scope.dialogStyle.height = attrs.height; 
      **scope.hideModal = function() { 
       scope.show = false; 
       scope.types.individual = "";** 
      }; 
     }, 
     template: "..." 
    }; 
}); 

確保您將$scope.types從控制器傳遞到指令。同樣當你經過show參數

可能是這樣的,

<model-dialog show="show" types="types"></model-dialog> 
+0

我已將我的模板添加到原始帖子。我有整個應用程序在

+0

你可以把你的html代碼放在你包含'' – DilumN

+0

的地方對不起,我發現我的錯誤。你的回答幫助了我。非常感謝你。 –

相關問題