简体   繁体   English

AngularJS:在编写HTML之前等待上下文被加载

[英]AngularJS: wait that context is loaded before write HTML

When the input select is loaded in an HTML form, sometimes the data get from the back-end is not ready and the select is displayed without any option selected. 当输入选择以HTML格式加载时,有时无法从后端获取数据,并且未选择任何选项就显示选择。

Could be possible to wait that the data is loaded before write the input select in the page? 在将输入选择写入页面之前,是否可以等待数据加载?

or there are any other way to select the right option depending on the angular value. 或者有其他方法可以根据角度值选择正确的选项。

PS. PS。 i can't change the data that i get from the back-end and that are una array for the all value and another variable with the selected option. 我无法更改从后端获取的数据,这些数据是所有值和带有选定选项的另一个变量的una数组。 The first one is always loaded correctly but sometimes the second one is empty when i want to select an option. 当我要选择一个选项时,第一个总是正确加载,但有时第二个是空的。

thanks 谢谢

Try to get access after event stateChangeSuccess 尝试在事件stateChangeSuccess之后获取访问权限

  $scope.$on('$stateChangeSuccess', function() {

            (function() {

            })();

        });

I assume you're using asynchronous methods to load the data. 我假设您正在使用异步方法加载数据。 In such case, the following should work. 在这种情况下,应采取以下措施。

First, have such markup: 首先,有这样的标记:

<div ng-show="loading">
    Loading, please wait...
    <!-- can also put gif animation instead -->
</div>

<select ng-hide="loading">...</select>

And in the controller: 并在控制器中:

$scope.loading = true;
GetData().then(function() {
    $scope.loading = false;
}, function() {
    $scope.loading = false;
    alert('error');
});

This assumes you load the data in a function that returns a Promise, you can of course just put the $scope.loading = false; 假设您将数据加载到返回Promise的函数中,那么您当然可以将$scope.loading = false;放进去$scope.loading = false; line in the proper location in your code, after the data is actually loaded. 实际加载数据后,在代码中的正确位置插入一行。

The effect will be that while $scope.loading is set to true , the user will see the "Loading" message while the drop down is hidden, and when you set it to false , the drop down will become visible while the "Loading" message will become hidden. 这样的效果是,当$scope.loading设置为true时 ,用户将在隐藏下拉菜单的同时看到“ Loading”消息,而当您将其设置为false时 ,在“ Loading”下拉列表中将可见消息将被隐藏。

That is how I fix this problem using AngularJS, Angular Resource & Ui-router to display selected object in an entity with Relationship: 这就是我使用AngularJS,Angular Resource和Ui-router在具有Relationship的实体中显示选定对象的方法:

Given that we have to entity in a simple relationship: 鉴于我们必须以简单的关系进行实体化:

Class: name(String), level(String). 类:名称(字符串),级别(字符串)。 ----> A class in school. ---->在学校上课。

Child: name(String), pseudo(String). 子级:名称(字符串),伪(字符串)。 ----> A Child. ---->一个孩子。

A child can be in one class at a time and there is many classes in school. 一个孩子可以一次上一堂课,而且学校里有很多课。 So We can have something like this(a One-To-One): 所以我们可以有这样的东西(一对一):

Class: name(String), level(String). 类:名称(字符串),级别(字符串)。 ----> A class in school. ---->在学校上课。

Child: name(String), pseudo(String), class(Class). 子级:名称(字符串),伪(字符串),类(类)。 ----> A Child. ---->一个孩子。

In my Ui-router state I do something like this when editing a Child: That is the state of the child to edit, when click on a link corresponding to it we query him and use a controller to resolve the entity related to him. 在我的Ui路由器状态下,当编辑子对象时,我会执行以下操作:这是要编辑的子对象的状态,当单击与之对应的链接时,我们会查询该子对象并使用控制器来解析与他相关的实体。

.state('child-edit', {
    parent: 'entity',
    url: '/child/{id:int}',
    views: {
        'content@': {
            templateUrl: 'path/to/chil/view/child-edit.html',
            controller: 'ChildEditController'
        }
    },
    resolve: {
        translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
            $translatePartialLoader.addPart('child');
            return $translate.refresh();
        }],
        entity: ['$stateParams', 'ChildService', function($stateParams, ChildService) {
            // We return the child to edit using a service.
            return ChildService.get({id : $stateParams.id});
        }]
    }
})

That is the controller I use to make this run normally: 那就是我用来正常运行的控制器:

angular.module('myApp').controller('ChildEditController',
    ['$scope', '$stateParams', '$q', 'entity', 'ClassService',
        function($scope, $stateParams, $q, entity, ClassService) {

        // We get all classes of school here.
            $scope.classes = ClassService.query();

        // That is the promise of child to edit get from resolve in state.
        $scope.childToEdit = entity;

        $q.all([$scope.classes.$promise, $scope.childToEdit.$promise]).then(function() {
          // When all data are resolved

          // In Js two objects with same properties and valyes but different memory allocation are different.
          // So I test value of Id before setting the right class of this child and angular will make able to edit
          // him in the UI with the ng-model
          var classOfChild = $scope.childToEdit.class;

          for (var k in $scope.classes) {
              if ($scope.classes[k].id === classOfChild.id) {
                  // We put the same reference of this class: then it will be selected in the UI of select box
                  $scope.childToEdit.class = $scope.classes[k];
              }
          }
        });
}]);

And the associated UI in HTML: 以及HTML中的相关UI:

<!-- The name of Child -->
<div class="form-group">
    <div class="col-md-4">
        <label for="field_child_name">Name of Child</label>
        <input type="text" class="form-control" name="name" id="field_child_name"
                       ng-model="childToEdit.name"
                       required />
    </div>
</div>

<!-- Selected class of child will be display here with all other classes available -->
<div class="form-group">
    <div class="col-md-4">
        <label for="field_child_class">Class of Child</label>
        <select class="form-control" id="field_child_class" name="class" ng-model="childToEdit.class" ng-options="class as class.name + ' : ' + class.level for class in classes">
            <option value=""></option>
        </select>
    </div>
</div>

Note: Hope it is the same situation where the selected data is not displaying because the references of querying class and property class in child object are different. 注意:希望与未显示所选数据的情况相同,因为子对象中查询类和属性类的引用不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM