使用检索到的ID初始化选择框

时间:2015-07-15 06:39:51

标签: angularjs

我是角度js的新手,我正在尝试编写一个包含选择框的编辑页面。它将id作为值和名称作为显示标签。

       <select class="form-control" ng-model="item.user" 
               ng-options="user.id as user.loginName for user in Users" ng-disabled="true" > 
    <!-- ng-init="item.user=userId" -->
    <option value="">Select User</option>                                             
</select>

当我添加编辑页面时,我从服务器端检索值,并返回选择框的id。当我使用ng-init设置值时(参见注释的ng-init代码),它只显示Select User选项。当我手动提供像

这样的用户ID时
ng-init="item.user=3"

相应的用户在选择框中正确显示。我尝试将控制器中的值设置为

$scope.item.user = result.userid;
alert($scope.item.user);

警报不会抛出,但当我在上面提醒result.userid时,它可以正常工作。可能是什么问题?

编辑1:

样本控制器(controller.js):

function SampleController(Sample, $scope, $http,$compile, $filter, $modal, $timeout, DTOptionsBuilder, DTColumnBuilder,DTInstances) {
$scope.edit = function(id) {
  //Retrieves value for dropdown
     selectbox.userList(function(result) {
        $scope.Users = result.content;
    });
        // alert("EDIT action initiated ID : " + id);
        var params = {
            id : id
        };

        $scope.sampleObj= Sample.view(params, function() {
            $scope.$state.go('sampleEdit', {id:id});
        });
    }
}

state(config.js):

.state('admin.editSample', {
              url: '/editSample/:id',
              templateUrl: 'sample/editSample.html',
              data: { pageTitle: 'Edit Sample'},
              controller: SampleEditController

          })

SampleEditController(controller.js):

var sampleEditController = function($stateParams, $scope, $http){
    var params = {};
    params.id = $stateParams.id;
    var url = "sample/edit";
    $http.get(url, {
        params : params
    }).success(function(result) {

        alert("Users: " + $scope.Users); //Here it alerts undefined
        for(var i = 0; i < $scope.Users.length && !$scope.item.user;i++){
            if ($scope.Users[i].id === result.id) {
                $scope.item.user = $scope.Users[i];
              }
        }
    })

编辑2: SampleRepository

@Repository
public interface IUserRepository extends JpaRepository<Employee, Long>{

@Query("select new com.sample.application.model.dto.SampleInfo(c.id,e.fullName,"
            + "c.accountExpired,c.accountLocked,c.rowCreated,c.enabled,c.rowAltered, c.password, c.passwordExpired, c.loginName, c.languageFluency) "
            + " from Employee c left join c.person e order by c.id asc")
    Page<IUserInfo> getList(Pageable pageable);
}

编辑3: services.js

.factory('sample', ['$http', function ($http) {
        return {
            userList : function(result){
                $http.get('sample/list').success(result);
            } }

我也在控制器里试过这个,

userRoleEditController:

$http.get(url, {
        params : params
    }).success(function(res) {
        selectbox.userList(function(result) {
            $scope.User = result.content;
            for(var i = 0; i < $scope.User.length; i++){

                if ($scope.User[i].id === res.empId) {
                    $scope.item.user = $scope.User[i];
                  }
            }
        });
    })

有了这个,我就能得到$ scope.User。我尝试在$ scope.item.user = $ scope.User [i]之后提醒$ scope.item.user;但它没有工作,我没有在下拉框中获得价值。

0 个答案:

没有答案