【问题标题】:Angular Js call function in ng-selectedng-selected中的Angular Js调用函数
【发布时间】:2018-03-02 07:47:53
【问题描述】:

我卡住了选择框预选值。

我有学生数据存储在数据库中,学科数据存储在这种形式: ["23","45","3"]

HTML 看起来像:

<select class="form-control selecBox"   name="classSubject[]" multiple required id="classSubj">
                                <option ng-repeat="subject in form.allsubjects" value="{{subject.id}}" ng-selected="form.classSubject.indexOf(subject.id)>-1">{{subject.subjectTitle}}</option>
                            </select>

JS 文件:

$scope.edit = function(id){
        showHideLoad();
        dataFactory.httpRequest('index.php/students/'+id).then(function(data) {

            //console.log("Say No To curruption:"+JSON.stringify(data, null,null));
            //console.dir(data)

            $scope.changeView('edit');
            $scope.form = data;
            //data.classSubject.toString();
            //data.classSubject=data.classSubject.replace("[", "");
            //data.classSubject=data.classSubject.replace("]", "");
            //data.classSubject=data.classSubject.split(",");
            //data.classSubject="16";
            //data.classSubject=data.classSubject.split(",");
            $scope.SelectedSubjects = data.classSubject;//"37";//[37,38,39];//data.classSubject[0];

            console.log(data.classSubject);
            showHideLoad(true);
        });
    }

PHP 控制器:

function fetch($id){
        $data = \User::where('role','student')->where('id',$id)->first()->toArray();
        $data['birthday'] = $this->panelInit->unix_to_date($data['birthday']);
        //$data['classSubject'] =($data['subjects']);
        $data['comVia'] = json_decode($data['comVia'],true);

        $data['classSubject']=json_decode($data['subjects'],true);


        if(!is_array($data['comVia'])){
            $data['comVia'] = array();
        }

        $data['academicYear'] = array();
        $academicYear = \academic_year::get();
        foreach ($academicYear as $value) {
            $data['academicYear'][$value->id] = $value->yearTitle;
        }

        $DashboardController = new DashboardController();
        $data['studentAcademicYears'] = array();
        $academicYear = \student_academic_years::where('studentId',$id)->orderBy('id','ASC')->get();
        foreach ($academicYear as $value) {
            $data['studentAcademicYears'][] = array("id"=>$value->academicYearId,"default"=>$value->classId,"defSection"=>$value->sectionId,"classes"=>\classes::where('classAcademicYear',$value->academicYearId)->get()->toArray(),"classSections"=>$DashboardController->classesList($value->academicYearId) );
        }
         $data['allsubjects']=$subjects=\DB::table('subject')->get();
         //$data['classSubject'] =json_encode($data['allsubjects'][12],true);

        return $data;
    }
enter code here

这里的问题是:如果 subject.id=3 那么选项“3”和“23”都选择了它必须只选择一个选项值(3)的选项

【问题讨论】:

  • 你能分享整个选择元素吗
  • 请添加更多您已经拥有的详细信息和代码
  • 我在下面的 cmets 中添加了更多屏幕截图

标签: angularjs


【解决方案1】:

在你的 ng-repeat 中使用 $index 跟踪 $index 将唯一标识数组中的项目。

【讨论】:

  • 能否请您告诉我如何放在这里:
  • 当我将 Array [ "9", "16" ] 的预选值从控制器传递到带有选择框 taga 的模板文件时,ng-selected="form.classSubject.indexOf(subject. id)>-1 现在没有选择任何内容
  • 我自己通过放置一对括号解决了这个问题 {{form.classSubject.indexOf(subject.id)>-1}}
【解决方案2】:

Template file

HTML 文件: PHP 控制器

PHP controller file image attached here

数据格式如下:["23","3"]

app.js file image attached here

【讨论】:

    猜你喜欢
    • 2017-04-02
    • 2017-01-16
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2016-12-17
    相关资源
    最近更新 更多