我正在尝试在指令中创建一个全新的范围变量。它只包含我的JSON数据的简化版本。我想避免在HTML中处理复杂的JSON对象,因此我试图创建一个只有指令才知道的JSON数据结构。
我想知道我们如何创建一个全新的变量?
我试过这样的事情,Angularjs在控制台中引发了一堆错误。我不确切地知道它在抱怨什么。
angularApp.directive('viewPersonData', function() {
return {
restrict: 'E',
replace: true,
scope: {
addressData: '=',
professionalData: '=',
creditData: '=',
},
templateUrl: 'viewPersonData.html',
link: linker
};
function linker(scope, element, attrs) {
scope.showData= function() {
if( something)
{
//defining a new scope variable
scope.employees = [];
var firstname = "John";
var lastname = "Smith";
var employee = {
"firstname": firstname,
"lastname": lastname
}
scope.employees.push(employee);
return true;
}
}
}
});
即使HTML模板(viewPersonData.html)确实通过以下代码检索员工数据。
<tbody ng-repeat="employee in employees">
<tr>
<td class="alnLt b" >{{employee.firstname}}</td>
<td class="alnLt b">{{employee.lastname}}</td>
</tr>
</tbody>
我在控制台中看到以下错误:
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: $watchCollectionWatch; newVal: 8; oldVal: 7","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 9; oldVal: 8","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 10; oldVal: 9","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 11; oldVal: 10","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 12; oldVal: 11","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n try {\r\n for(var i = 0, ii = length, part; i<ii; i++) {\r\n if (typeof (part = parts[i]) == 'function') {\r\n part = part(context);\r\n if (part == null || part == undefined) {\r\n part = '';\r\n } else if (typeof part != 'string') {\r\n part = toJson(part);\r\n }\r\n }\r\n concat[i] = part;\r\n }\r\n return concat.join('');\r\n }\r\n catch(err) {\r\n var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n $exceptionHandler(newErr);\r\n }\r\n }; newVal: \"Smith\"; oldVal: undefined"]]
at Error (native)
at Object.Scope.$digest (angular.js:8841:61)
at Object.Scope.$apply (angular.js:9012:24)
at done (angular.js:10265:45)
at completeRequest (angular.js:10449:7)
at XMLHttpRequest.xhr.onreadystatechange (angular.js:10404:11)
答案 0 :(得分:2)
删除链接器并使用指令的控制器属性
controller: function($scope){
$scope.employees = [{
"firstname": firstname,
"lastname": lastname
}];
}
这将创建员工数据结构。
答案 1 :(得分:0)
这是一个工作的掠夺者Example New Variable。
我进行了设置,以便在单击链接时调用showData()。我建议仅将控制器用于初始化。在@Carlos建议的控制器内部初始化员工没有什么害处,但如果不需要,那么您可以在链接中的showData方法中创建它。
指令:
<custom></custom>
如果您遇到其他问题,请检查您的指令设置。