简体   繁体   English

父指令范围内不可用的angular指令变量

[英]angular directive variable not available in parent scope

I am currently stuck on hopefully a simple problem: 我目前被困在希望一个简单的问题:

I have a controller.js 我有一个controller.js

app.controller('WizardCtrl', function ($scope) {
  $scope.send = function() {
    console.log($scope.isPasswordChanged)
  }
});

My view.html 我的view.html

isPasswordChanged: {{ isPasswordChanged }} <br>
<update-password-field is-changed="isPasswordChanged"></update-password-field>
<button ng-click="send()"></button>

My directive.js 我的指令.js

app.directive('updatePasswordField', function () {
  return {
    restrict: 'E',
    scope: {
      isChanged: "="
    },
    templateUrl: "password-field-directive.html",
    link: function (scope) {
        // some magic to set isChanged value to true of false 
    }
  }
});

So in my view.html I can see the changes of "isPasswordChanged", to it is somehow in my $scope, but if I console.log($scope) the "isPasswordChanged" is not present. 所以在我的view.html中我可以看到“isPasswordChanged”的变化,以某种方式在我的$ scope中,但是如果我在console.log($ scope)中,则“isPasswordChanged”不存在。

Why and how to make it present? 为什么以及如何让它出现?

I think you need to include the $scope in your controller's function(). 我认为你需要在控制器的函数中包含$scope ()。 In other words: 换一种说法:

app.controller('WizardCtrl', ['$scope', function ($scope) {

Otherwise I don't believe it will work. 否则我不相信它会起作用。

Turned out it is a problem with probably my structure. 原来这可能是我的结构问题。

I changed 我变了

is-confirmed="isPasswordConfirmed"

to

is-confirmed="ui.isPasswordConfirmed"

"ui" is an empty object I defined in my $scope. “ui”是我在$ scope中定义的空对象。

And its working. 它的工作。 I tried a stand alone example 我尝试了一个独立的例子

http://plnkr.co/edit/bkMZTEWgkrmVfxqiYCcB?p=preview

But it is working without this changes, it also might be a mismatch with angular version. 但它没有这种变化,它也可能与角度版本不匹配。

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

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