Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用工厂/服务的ng选项的默认ng模型值_Angularjs - Fatal编程技术网

Angularjs 使用工厂/服务的ng选项的默认ng模型值

Angularjs 使用工厂/服务的ng选项的默认ng模型值,angularjs,Angularjs,作为一个新的AngularJS用户,我有一个表单,其中填充了如下数据: 我的EntriesFactory.js 填充EntryDetails.cfm表单 最后,这里是我的EntryDetailsController.js 我想要的是,在默认情况下选择{{detail.LAB}}}值,并将实验室中的值作为其他选项 非常感谢您的帮助。一旦您从服务中获得所有实验室,您需要在范围上手动设置details.LAB变量,该变量应指向实验室数组中的元素 WistarService.getLabs() .

作为一个新的AngularJS用户,我有一个表单,其中填充了如下数据:

我的EntriesFactory.js

填充EntryDetails.cfm表单

最后,这里是我的EntryDetailsController.js

我想要的是,在默认情况下选择{{detail.LAB}}}值,并将实验室中的值作为其他选项


非常感谢您的帮助。

一旦您从服务中获得所有实验室,您需要在范围上手动设置details.LAB变量,该变量应指向实验室数组中的元素

WistarService.getLabs()
   .success(function (labs){
      $scope.labs = labs;
      $scope.details.LAB= labs.filter(function(){ // some condition to filter})[0];
    })
ng模型指向details.LAB

更新:根据你的评论,应该有一些关键点来匹配实验室和入口。因此,你应该做一些类似的事情

$scope.details.LAB= labs.filter(function(lab){
   lab.id=$scope.entry.id; 
)[0];

但请记住,只有在条目数据可用时才能执行此操作。因此,要么链接http操作,要么在条目上放置一个监视,并在监视中添加该逻辑。

但是$scope.labs如何知道$scope.entry传递了什么?因为这就是我想要选择的值的来源。根据你的建议,我让自己熟悉了w/$watch和w/filter,而不是通过服务获得值,我得到了detail.LAB值两次。
(function () {
    "use strict";
    var WistarService = function ($http) {        
        this.getLabs = function () {
            return $http.get('ABC/XYZ.cfc?method=GetLabs&returnformat=json&queryformat=struct');
        };
    };

    WistarService.$inject = ['$http'];

    angular.module('appDHCP')
        .service('WistarService', WistarService);

}());
(function () {
    "use strict";
    var EntryDetailsController = function ($scope, $routeParams, EntriesFactory, WistarService) {
        var addressId = $routeParams.REC_ID;
        $scope.entry = null;
        $scope.labs = null;

        //function init() {
            EntriesFactory.getEntry(addressId)
                .success(function (entry) {
                    $scope.entry = entry;
                })
                .error(function (data, status, headers, config) {
                //handel errors
                });
            WistarService.getLabs()
                .success(function (labs){
                    $scope.labs = labs;                                                             
                })
                .error(function (data, status, headers, config) {
                //handel errors
                });
        /*
        }

        init();
        */

    };

    EntryDetailsController.$inject = ['$scope', '$routeParams', 'EntriesFactory', 'WistarService'];

    angular.module('appDHCP')
        .controller('EntryDetailsController', EntryDetailsController);

}());
WistarService.getLabs()
   .success(function (labs){
      $scope.labs = labs;
      $scope.details.LAB= labs.filter(function(){ // some condition to filter})[0];
    })
$scope.details.LAB= labs.filter(function(lab){
   lab.id=$scope.entry.id; 
)[0];