我创建了一个简单的“模态对话”指令,它使用转录。我想在“模态对话”指令中放置一个表单()。我期望formController放置在指令内,可以在父控制器的范围内访问,但是它不是。请看下面的小提琴:
http://jsfiddle.net/milmly/f2WMT/1/
完整代码:
<!DOCTYPE html> <html> <head> <title>AngJS test</title> <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/foundation/4.0.9/css/foundation.min.css"> <style> .reveal-modal { display: block; visibility: visible; } </style> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('app',[]); app.controller('appCtrl',function ($scope) { $scope.model = { id: 1,name: 'John' }; $scope.modal = { show: false }; }); app.directive('modal',function () { return { scope: { show: '=' },transclude: true,replace: true,template: '<div class="reveal-modal small" ng-show="show"><div class="panel" ng-transclude></div></div>' } }); </script> </head> <body ng-app="app"> <div ng-controller="appCtrl"> <div class="panel"> Id: {{ model.id }}<br> Name: {{ model.name }}<br> Controller formController: {{ form }}<br> Directive formController: {{ myForm }}<br> </div> <form name="form" class="panel"> <input type="text" ng-model="model.name"> </form> <a ng-click="modal.show=!modal.show">toggle dialog</a> <div modal show="modal.show"> <form name="myForm"> <input type="text" ng-model="model.name"> </form> </div> </div> </body> </html>
所以我的问题是如何访问或者是否可以从父控制器访问指令的formController?
谢谢你的答案。
-米兰
因为您正在使用转义,所以该指令将创建一个子代的范围。从控制器范围(003)到指令的转录范围(005)没有简单的路径:
(硬/不推荐的路径是通过私有属性$$ childHead在控制器范围上,找到隔离范围,然后使用$$ nextSibling获取到转发的范围。)
更新:
从this answer的见解,我想我们可以在指令中获取formController,然后使用=来获取父对象。
scope: { show: '=',formCtrl: '=' },... link: function(scope,element) { var input1 = element.find('input').eq(0); scope.formCtrl = input1.controller('form'); }
HTML:
<div modal show="modal.show" form-ctrl="formCtrl">