簡體   English   中英

父指令范圍內不可用的angular指令變量

[英]angular directive variable not available in parent scope

我目前被困在希望一個簡單的問題:

我有一個controller.js

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

我的view.html

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

我的指令.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 
    }
  }
});

所以在我的view.html中我可以看到“isPasswordChanged”的變化,以某種方式在我的$ scope中,但是如果我在console.log($ scope)中,則“isPasswordChanged”不存在。

為什么以及如何讓它出現?

我認為你需要在控制器的函數中包含$scope ()。 換一種說法:

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

否則我不相信它會起作用。

原來這可能是我的結構問題。

我變了

is-confirmed="isPasswordConfirmed"

is-confirmed="ui.isPasswordConfirmed"

“ui”是我在$ scope中定義的空對象。

它的工作。 我嘗試了一個獨立的例子

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

但它沒有這種變化,它也可能與角度版本不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM