ng-model中控制器中的未定义参数

时间:2019-07-08 16:05:59

标签: javascript angularjs

我从API获得了数据,用于显示表单。表单包含级联选择字段。我有2个函数用下一个选择的结果过滤数组,但我无法从ng-model中读取属性。

例如,我选择第一个选项并获取它的ID,然后需要将该ID传递给控制器​​中的函数,后者可以过滤数组以返回适当的选项。

API数据

Set db = session.CurrentDatabase
Set uiview = ws.CurrentView

Set uidoc = ws.CurrentDocument
Set dialogDoc = uidoc.Document
Set view = db.GetView("Computer")

'First run through the loop checks if ANY doc has PStatus = "Lock"
Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
    If doc.PStatus(0) = "Lock" Then
        Msgbox "Complete PC Inspection first!"
        Exit Sub
    End If
    Set doc = view.getNextDocument(doc) 'add (doc)
Wend

'Not sure what this is all about, but I'll leave it in
answer% = Messagebox("Do you confirm?")
If Not answer% = 6 
    Msgbox("Process Incomplete")
    Exit Sub
Else
    'I've no idea what the next three lines are for, but I'll leave them alone
    dialogDoc.Form = "BatchInfo"
    Call uidoc.FieldSetText("SaveOptions", "1")
    Call uidoc.Save

    'Loop through the view again, creating copies and setting status to Lock
    Set doc = view.GetFirstDocument 'Add this line
    While Not (doc Is Nothing)
        If doc.PStatus(0) = "Active" Then

            '-----create new copy document-----'
            Set newdoc = doc.CopyToDatabase(db)
            newdoc.PBatchNo = dialogDoc.BBatchNo(0)
            newdoc.PStatus = "Draft"
            Call newdoc.Save(True, False)

            doc.PStatus = "Lock"
            Call doc.ComputeWithForm(False,False)
            Call doc.save(True,False)
        End If
        Set doc = view.GetNextDocument(doc)
     Wend
End If
Messagebox("Process completed.")

服务

section: { id: "111", name: "Section1" }
nomination: { id: "666", name: "Nomination2" }
category: { id: "999", name: "Category2"}

控制器

const allSections = [
    { id: '111', section: 'Section1' },
    { id: '222', section: 'Section2' },
    { id: '333', section: 'Section3' },
];
const allNominations = [
    { id: '555', sectionId: '111', nomination: 'Nomination1' },
    { id: '666', sectionId: '222', nomination: 'Nomination2' },
    { id: '777', sectionId: '333', nomination: 'Nomination3' },
];
const allCategories = [
   { id: '999', sectionId: '111', category: 'Category1' },
   { id: '888', sectionId: '222', category: 'Category2' },
   { id: '000', sectionId: '333', category: 'Category3' },
];
getSection() {
   return allSections;
},
getSectionNomination(sectionId) {
   const nominations = ($filter('filter')(allNominations, { sectionId }));
   return nominations;
},
getNominationCategory(sectionId) {
   const categories = ($filter('filter')(allCategories, { sectionId }));
   return categories;
},

查看

$scope.award = {};
$scope.sections = awards_service.getSection();
$scope.getSectionNominations = () => {
    $scope.nominations = 
    awards_service.getSectionNomination($scope.award.section.id);
    $scope.categories = [];
};
$scope.getNominationCategories = () => {
    $scope.categories = 
    awards_service.getNominationCategory($scope.award.section.id);
};

我尝试从API数据中获取选定的ID并过滤我的数组。在视图中,我可以看到正确的ID为award.section.id,但是当我通过该papameter在控制器中运行时,会得到不确定的结果。 $ scope.award.section.id的定义不足。

1 个答案:

答案 0 :(得分:1)

尝试将select的当前值传递给函数:

$scope.sectionId = 0;
if ($scope.sections && $scope.sections[0]) {
  $scope.sectionId = $scope.sections[0].id;
}
$scope.getSectionNominations = (sectionId) => {
    if (!sectionId) return;

    $scope.nominations = awards_service.getSectionNomination(sectionId);
    $scope.categories = [];
};

和html中的

<select 
  ng-model="sectionId"
  ng-change="getSectionNominations(sectionId)" 
  ng-options="object.id as object.section for object in sections"
>
</select>

参见将变量作为参数传递

中的答案here