我有一个指令,类似的东西:
.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)
.controller('QuestionIndexController', function ($scope, $routeParams) {
$scope.data = {};
$scope.data.productTypeUrl = $routeParams.stringUrl;
$scope.data.arrayFieldsDisplay = [];
$scope.question_array = {};
})
Son控制器(名称文件:question.js):
.controller('QuestionFormController', ['$scope', '$location', 'questionApiService', function($scope, $location, questionApiService ){
var d = new Date();
$scope.data.jsonField = {};
$scope.data.yearStart = d.getFullYear() - 90;
$scope.data.yearEnd = d.getFullYear() - 18;
questionApiService.getField()
.then(function(response) {
$scope.data.jsonField = response.data;
$scope.data.arrayFieldsDisplay.push($scope.data.jsonField['nextQ']);
});
Son指令(在其他文件中):
.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);
我想,如果我进入" questionApiService.getField()"我的变量" $ scope.data.jsonField"该值必须保留在mi指令中使用它的范围内。我的逻辑错了?