0
我创建了2个指令,login
和rankSelector
。正如你从代码中看到的,登录用在rankSelector
之内。但我不确定在这两个指令之间实现通信的正确方法是什么? rankSelector
是login
的父亲,需要知道login
指令中的某些更改何时发生。 ATM我使用rankSelector
中的控制器,它有passwordsMatch
方法login
需要通知父指令有关更改的方法。代码如下:angularJS中paren-child指令之间的通信
<script type="text/ng-template" id="rankSelector.html">
<form name="rankings" novalidate>
<div>
<div login></div>
<!-- if passwords match show this -->
<input type="text" ng-model="someOtherField" name="someOtherField" ng-show="passwordsMatch" ng-hide="!passwordsMatch" />
</div>
</form>
</script>
我的指令:
app.directive("login", [
function(loginService) {
return {
templateUrl: "login.html",
replace: true,
require: "?^rankSelector",
scope: true,
link: function(scope, elem, attrs, ctrl) {
scope.$watch("confirmPassword", function(newValue, oldValue) {
// also how in here I can check if scope.password is $valid without douing checks manually?
ctrl.passwordsMatch(scope.password && scope.password === scope.confirmPassword);
});
}
};
}
]);
app.directive("rankSelector", [
function(rankingsService, $rootScope) {
return {
templateUrl: "rankSelector.html",
replace: true,
controller: ["$scope",
function($scope) {
this.passwordsMatch = function(showConfirm) {
$scope.passwordsMatch = showConfirm;
}
}
],
scope: true,
link: function(scope, elem, attrs) {
scope.passwordsMatch = false;
}
};
}
]);
不知道如果我在这里做正确的事情login
指令需要知道它将被插入到什么控制器中。这种方法有什么明显的问题需要注意?
另外我怎么可能会做login
指令里面的检查$watch
打电话查看是否字段是$valid
等?