[英]Angular scope.$watch not working in directive
我有一個帶scope.$watch
的角度指令,它不起作用,但是我知道值在變化。 這是指令:
var StepFormDirective = function ($timeout, $sce, dataFactory) {
return {
replace: false,
restrict: 'AE',
scope: {
currentStep: "=",
title: "="
},
template: '<h3>{{title}}</h3><form class="step-form"></form>',
compile: function (tElem, attrs) {
return function (scope, elem, attrs) {
scope
.$watch(
function(){return scope.currentStep;},
function (newValue) {
var stepFormFields = Array();
stepFormFields.push($sce.trustAsHtml("<p>Fields</p>"));
alert(scope.currentStep);
}
);
};
}
};
};
標簽是:
<div title="'test'" currentStep="currentContext.currentStep"></div>
我知道currentContext.currentStep
正在更改,因為我的頁面上也有此內容,並且它會更新:
<pre>{{currentContext.currentStep | json}}</pre>
警報第一次被調用,但是當值更改時(由pre
標簽中的位證明),警報不再被調用,並且我沒有js控制台錯誤。
步驟的輸出(它的數據類型)為:
{
"identifier": "830abacc-5f88-4f9a-a368-d8184adae70d",
"name": "Test 1",
"action": {
"name": "Approval",
"description": "Approve",
"instructions": "Select 'Approved' or 'Denied'",
"validOutcomes": [
{
"outcome": "Approved",
"display": "Approved",
"id": "Approved"
},
{
"outcome": "Denied",
"display": "Denied",
"id": "Denied"
}
]
...
嘗試將$watch
方法與第三個參數設置為true( objectEquality
)一起使用:
$watch('currentStep', function(newValue){...}, true);
或使用$watchCollection
:
$watchCollection('currentStep', function(newValue){...})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.