Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
Javascript AngularJS:在编写HTML之前,等待加载上下文_Javascript_Jquery_Html_Angularjs - Fatal编程技术网

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

Javascript AngularJS:在编写HTML之前,等待加载上下文,javascript,jquery,html,angularjs,Javascript,Jquery,Html,Angularjs,在HTML表单中加载输入选择时,有时从后端获取的数据未准备就绪,并且选择显示时未选择任何选项 在页面中写入输入选择之前,是否可以等待数据加载 或者,根据角度值,有任何其他方法可以选择正确的选项 另外,我无法更改从后端获取的数据,这些数据是all值的una数组和具有所选选项的另一个变量。第一个始终正确加载,但有时当我要选择一个选项时,第二个总是空的 谢谢在事件状态更改成功后尝试访问 $scope.$on('$stateChangeSuccess', function() {

在HTML表单中加载输入选择时,有时从后端获取的数据未准备就绪,并且选择显示时未选择任何选项

在页面中写入输入选择之前,是否可以等待数据加载

或者,根据角度值,有任何其他方法可以选择正确的选项

另外,我无法更改从后端获取的数据,这些数据是all值的una数组和具有所选选项的另一个变量。第一个始终正确加载,但有时当我要选择一个选项时,第二个总是空的


谢谢

在事件状态更改成功后尝试访问

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

            (function() {

            })();

        });

尝试在事件状态更改成功后获取访问权限

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

            (function() {

            })();

        });

我假设您正在使用异步方法加载数据。在这种情况下,以下方法应该有效

首先,有这样的标记:

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

<select ng-hide="loading">...</select>
这假设您将数据加载到一个返回承诺的函数中,您当然可以将
$scope.load=false行在代码中的正确位置


其效果是,当
$scope.loading设置为true时,用户将在下拉列表隐藏时看到“loading”消息,当您将其设置为false时,下拉列表将变为可见,而“loading”消息将变为隐藏。

我假设您正在使用异步方法加载数据。在这种情况下,以下方法应该有效

首先,有这样的标记:

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

<select ng-hide="loading">...</select>
这假设您将数据加载到一个返回承诺的函数中,您当然可以将
$scope.load=false行在代码中的正确位置


其效果是,当
$scope.loading设置为true时,用户将在下拉列表隐藏时看到“loading”消息,当设置为false时,下拉列表将可见,而“loading”消息将隐藏。

这就是我使用AngularJS解决此问题的方法,角度资源和Ui路由器,用于在具有关系的实体中显示选定对象:

鉴于我们必须以简单的关系建立实体:

类别:名称(字符串)、级别(字符串)。----->学校的一个班级

子项:名称(字符串),伪(字符串)。----->一个孩子

一个孩子一次可以上一个班,学校里有很多班。 所以我们可以这样(一对一):

类别:名称(字符串)、级别(字符串)。----->学校的一个班级

子项:名称(字符串)、伪(字符串)、类(类)。----->一个孩子

在我的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});
        }]
    }
})
这是我用来正常运行的控制器:

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];
              }
          }
        });
}]);
以及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>

子女姓名
儿童阶级

注意:希望所选数据不显示的情况相同,因为子对象中查询类和属性类的引用不同。

这就是我如何使用AngularJS、AngularResource和Ui router在具有关系的实体中显示所选对象来解决此问题的方法:

鉴于我们必须以简单的关系建立实体:

类别:名称(字符串)、级别(字符串)。----->学校的一个班级

子项:名称(字符串),伪(字符串)。----->一个孩子

一个孩子一次可以上一个班,学校里有很多班。 所以我们可以这样(一对一):

类别:名称(字符串)、级别(字符串)。----->学校的一个班级

子项:名称(字符串)、伪(字符串)、类(类)。----->一个孩子

在我的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});
        }]
    }
})
这是我用来正常运行的控制器:

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];
              }
          }
        });
}]);
以及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>

子女姓名
儿童阶级

注意:由于子对象中查询类和属性类的引用不同,希望所选数据不显示的情况相同。

您可以
ng在数据不在位置时禁用控件。使用
ng disabled='!data'
或create a flag`requestIsReady,并在ajax成功或最终成功时将其设置为true。我已经了解到这种情况,但请注意,需要更多描述才能更好地帮助您。你在使用Ui路由器吗?您能举例说明如何获取数据并显示它吗?您可以在数据不存在时禁用控件。使用
ng disabled='!data'
或create a flag`requestIsReady,并在ajax成功或最终成功时将其设置为true。我已经了解到这种情况,但请注意,需要更多描述才能更好地帮助您。你在使用Ui路由器吗?你能写一个你如何获取数据并显示它的例子吗?谢谢,这对一些有角度的代码行很好,但是我能用它来写HTML或者简单地选择选项吗?谢谢,这对一些有角度的代码行很好,但是我能用它来写HTML或者简单地选择选项吗?