简体   繁体   English

部分隔离的示波器角度

[英]Partially isolated scope angular

I have a directive that i want to include multiple times on a page. 我有我想包括在页面上多次 指令
I also want to control this directive from the parent controller . 我也想从父控制器控制此指令。

To give an example, the directive is a modal has a transcluded partial in it. 举个例子,该伪指令是一个模态,其中包含有一个被包含的部分。 This partial has a form that sends a message. 该部分具有发送消息的形式。

I want to dictate what that form does from my parent controller however when i isolate the scope (so that i can have more than one of these directives per page) i can no longer access the form object that angular creates. 我想指示该表单从我的父控制器执行什么操作,但是当我隔离范围时(这样我每页可以有多个这些指令之一),我将无法再访问angular创建的表单对象。

im open to suggestions 我愿意接受建议

html page HTML页面

<page controller="pageCtrl">
  <modal partial="/path/to/partial1"></modal>
  <modal partial="/path/to/partial2"></modal>
</page>

html partial 1&2 html部分1&2

partial1.html
  <form name="formName1"></form>
partial2.html
  <form name="formName1"></form>

JS JS

app.directive('modal', function(){
  return {
    restrict: 'AE',
    transclude: true,
    replace: true,
    scope:true,
    templateUrl: '/path/to/modal.html',
    link:function($scope, elem, attrs){
      $http.get(attrs.partial, { cache: $templateCache })
      .success(function(response) {
        element.find('[ng-transclude]').append(response);
        $compile(element.find('[ng-transclude]').contents())($scope);
      });
    }
  }
});
app.controler('pageCtrl', function(){
  $scope.submitForm1 = function(data){
    if($scope.formName1.$valid){
      //DO SOMETHING
    }
  }
})

OK so after typing it all out i realized how to do such a thing. 好的,所以在输入全部内容后,我意识到该怎么做。
My goal was to isolate the scope of the directive but not the scope of the form inside. 我的目标是隔离指令的范围,而不是隔离内部表单的范围。
So all i had to do was change what scope my included form/modal content was compiling against; 因此,我要做的就是更改我所包含的表单/模式内容的编译范围;

$compile(element.find('[ng-transclude]').contents())($scope.$parent);
Instead of; 代替;
$compile(element.find('[ng-transclude]').contents())($scope);

Thanks anyway 不管怎么说,还是要谢谢你

-James Harrington -詹姆斯·哈灵顿

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM