【问题标题】:passing parameters to state is causing element to not show将参数传递给状态导致元素不显示
【发布时间】:2018-02-20 15:32:41
【问题描述】:

在我的 AngularJS 应用程序中,我有一个表单,用户需要在其中输入应用程序编号,如果成功,则显示内容,其下方会显示适当的数据。如果我将这些数据作为参数传递给当前的state,我只能通过scope 访问这些数据。我正在使用ui-router 进行路由。

我已阅读诸如Reloading current state - refresh data 之类的线程,它建议我可以将数据传递到当前状态而不刷新页面,但它会不断刷新并且内容不显示。我也不确定我是否正确使用了ng-show 和/或ng-if。我传递数据的函数如下:

vm.findPatent = function(patentNo) {
    searchPatentService.findPatent(patentNo)
    .then(
        function(data) {
            $state.go('.', { patent: data }, {reload: false, notify: false, location: false})
            })
        },
        function(errResponse) {
            console.error('Error while finding patent');
        }
    );
}

还有我在state内的参数:

.state('search-patent', {
    url: '/search-patent',
    component: 'searchpatent',
    params: {
        navigation: 'patentnav',
        patent: null
    }
})

我对在表单提交时显示以下内容的表单的看法:

<form ng-submit="$ctrl.findPatent(searchPatent)" name="searchPatentForm">
    <fieldset>
        <div class="form-group row">
            <labelfor="searchPatent">EP No.</label>
            <div>
                <input type="text" name="searchPatent" ng-model="searchPatent" class="form-control" placeholder="Search" required>
            </div>
        <div>
            <button type="submit">Submit</button>
        </div>
    </div>
  </fieldset>
</form> 

<div class="row" ng-show="searchPatentForm.$submitted">
    <div class="col-md-12">
//ANOTHER FORM AND CONTENT WHICH IS DISPLAYED ON SUBMIT
    </div>
</div>

问题

如何在不刷新页面的情况下将数据传递给当前的state?另外,有没有更好的解决方案来使用ng-show?谢谢

【问题讨论】:

  • 还不清楚你的目标是什么?你想刷新页面 onClick ??并显示一些数据??
  • 为什么你不愿意使用 $scope ??
  • 道歉。所以在提交时,我需要将参数传递给当前状态并保持在当前状态和页面上,这样我就可以在提交时显示(通过 ng-show)的容器中显示必要的对象属性。我遇到的问题是,它传递了参数,但页面刷新,并且要显示的包含,闪烁一秒钟然后消失
  • 我愿意,我在视图中使用vm$ctrl,但如果您有一个可以帮助我的示例,将不胜感激
  • 看我的回答这应该可以@Patrick

标签: javascript angularjs forms angular-ui-router


【解决方案1】:

因为您想在提交后留在页面上。

$scope.result= null;
vm.findPatent = function(patentNo) {
    searchPatentService.findPatent(patentNo)
    .then(
        function(data) {
            $scope.result=data;
            })
        },
        function(errResponse) {
            console.error('Error while finding patent');
        }
    );
}

在你看来,如果你想重新创建你的 div,你应该使用ng-if,否则使用ng-showsee details here

<div class="row" ng-if="result">
    <div class="col-md-12">
      {{result}}
    </div>
</div>

详细示例见here

【讨论】:

  • 啊,太棒了,简单多了。虽然唯一的问题是容器在初始表单提交之前显示?如何在用户点击提交之前隐藏它?
  • 已排序。添加了&amp;&amp; searchPatentForm.$submitted。感谢您抽出宝贵时间@Abdullah
  • Np 你只能使用'$scope.result.length'
  • 对不起是什么意思?
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 2019-12-20
  • 2021-12-25
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多