【发布时间】:2016-03-11 08:50:06
【问题描述】:
我有一个对象的 json 文件,其中存储要在指令中使用的属性。 我想在指令中使用 json obj 模型值,但我没有尝试任何工作。
有人知道我做错了什么/错过了什么吗?我觉得这很令人困惑。 希望有人可以帮助尝试这几天!
编辑:: 我有一个 $http 服务,它获取并返回 Json 对象,我可以正常访问属性。
我特意尝试使用json obj模型属性的值——“model”:“ticketData.contactname”作为ng-model的动态值。
如果我只使用 ticketData.contactname obj,那么它工作正常,我可以编辑模型值,但如果我尝试使用 Json obj 中的字符串,那么它只会将字符串打印到输入框中。
我不知道该怎么办。我确信这是我所缺少的基本内容。
提前致谢
Json 示例:
[
{
"inputsContact" : [
{
"labelName" : "Contact Name",
"placeholder" : "Enter your name",
"model" : "ticketData.contactname",
"type" : "text"
}
}
]
HTML 示例:
<text-input-comp inputdata="contactName" ng-model="contactModel"> </text-input-comp>
指令 text-input-comp:
.directive('textInputComp', [ '$compile', function($compile){
return {
restrict: 'E',
scope: {
inputData: '=inputdata',
modelData: '=ngModel'
},
templateUrl: '/app/views/partials/components/textInputComp.html'
}
}]);
指令模板示例:
<label> {{ inputData.labelName }} </label>
<input type="text" ng-model="modelData" ng-model-options="{ getterSetter: true }" placeholder="{{ inputData.placeholder }}" />
<div ></div>
控制器示例:
$scope.contactName = $scope.inputData[0].inputsContact[0];
$scope.contactModel = $scope.inputData[0].inputsContact[0].model;
【问题讨论】:
标签: angularjs json directive angular-ngmodel