2017-03-02 124 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

的地方对不起,我发现我的错误。你的回答帮助了我。非常感谢你。 –

相关问题