指令angularjs中的值丢失
我有一个指令,类似这样的:指令angularjs中的值丢失,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有一个指令,类似这样的: .directive('reasonField', ['questionApiService', function(questionApiService) { var id = 1; return { require: 'ngModel', restrict: 'A', link: function (scope, element, attrs, ctrl) { scope.$watch(attrs.ngModel, functio
.directive('reasonField', ['questionApiService', function(questionApiService) {
var id = 1;
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(val) {
console.log(scope.data);
});
}
};
}]);
{ "arrayFieldsDisplay": [ 1 ], "jsonField": { "listQ": [ 1 ], "answQ": {}, "nextQ": 1 }, "yearStart": 1925, "yearEnd": 1997 }
当我看到控制台打印时,显示如下内容:
.directive('reasonField', ['questionApiService', function(questionApiService) {
var id = 1;
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(val) {
console.log(scope.data);
});
}
};
}]);
{ "arrayFieldsDisplay": [ 1 ], "jsonField": { "listQ": [ 1 ], "answQ": {}, "nextQ": 1 }, "yearStart": 1925, "yearEnd": 1997 }
如果我尝试执行console.log(scope.data.yearStart),控制台将正确返回值,但如果我尝试打印console.log(scope.data.jsonField),则不会返回任何内容
我不明白怎么了
先谢谢你
编辑1
控制器逻辑为:
父控制器(名称文件:question.js)
Son控制器(名称文件:question.js):
Son指令(在其他文件中):
我想,如果我将变量“$scope.data.jsonField”设置为“questionApiService.getField(),则该值必须保留在范围内,以便在mi指令中使用。我的逻辑错了 记录数据的其他属性时会发生什么?嗨@camden_kid。我正在检查,“arrayFieldsDisplay”的值不显示在屏幕上。这组值在previus控制器中。@camden_kid,谢谢,请检查我的编辑1:)对不起,我看不出您的问题的解决方案是什么。祝你好运别担心,谢谢你。
.directive('reasonField', ['questionApiService', function(questionApiService) {
var id = 1;
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(val) {
//alert(scope.data.yearStart);