在UI的SREF一个前pression错误 [英] Have a expression error in ui-sref

查看:166
本文介绍了在UI的SREF一个前pression错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过UI路由器文档工作。我有什么,我试图用UI的SREF做一个体面的感觉。在底部的 TD 的我加入的ui-SREF到所需HREF。我确信打电话,我想火的状态,括号是我试图创建路由PARAMS。

问题是,虽然是我得到的语法错误:令牌'。在列{2}的前pression的[{3}]开始[{4}] 从角文档。

我确信引用一些额外的信息柜面我在code的任何部分,很想念。

 < D​​IV CLASS =行>  < D​​IV CLASS =面板面板主要>    < D​​IV CLASS =面板标题>
      客户名单
    < / DIV>    < D​​IV CLASS =面板体>
      过滤:LT;输入类型=文本NG模型=customerFilter>
    < / DIV>    <表类=table表条纹桌响应>
      &所述; TR>
        百分位NG点击=ctrl.doSort('名')>名称和LT; /第i
        百分位NG点击=ctrl.doSort(城市)>城市< /第i
        百分位NG点击=ctrl.doSort('的OrderTotal')>订单总额< /第i
        百分位NG点击=ctrl.doSort('加盟')>加入< /第i
        <第i&安培; NBSP;< /第i
      < / TR>      < TR数据-NG-重复=卡斯特在ctrl.customers |过滤器:customerFilter |排序依据:ctrl.sortBy:ctrl.reverse>
        &所述; TD> {{cust.name |大写}}< / TD>
        &所述; TD> {{cust.city}}&下; / TD>
        &所述; TD> {{cust.orderTotal |货币}}< / TD>
        < TD> {{cust.joined |日期}}< / TD>        < TD><一个UI的SREF =命令({} ctrl.cust.id)>查看订单< / A>< / TD>
      < / TR>    < /表>  < / DIV>
    <跨度个总客户:{{ctrl.customers.length}}< / SPAN>< / DIV>

下面是我的控制器的顶部。我与controllerAs工作,试图让更多习惯了约翰爸爸风格指南

 
    .module('app.customers')
    .controller('CustomerController',CustomerController);功能CustomerController($ stateParams){
    VAR VM =这一点;
//客户ID来自URL参数
    的console.log($ stateParams);
    VAR的customerId = $ stateParams.customerId;
    vm.orders = NULL;

我找回一个空的对象$ stateParams

我的路由文件被分解为具体的,因为我可以做到这一点。我创建了一个视图对象,创建一个主视图,并在HTML中引用它。我做了一个决心对象,将采取$ stateParams

 
    .module('app.customers')
    的.config(配置);功能配置($ stateProvider){
    的console.log('客户路线)
    $ stateProvider
        .STATE('客户',{
            网址:'/客户,
            观点:{
                主@:{
                    templateUrl:./components/customers/customers.html',
                    控制器:'CustomerController',
                    controllerAs:CTRL
                }
            },
            解析:{
                客户ID:['$ stateParams',函数($ stateParams){
                    返回$ stateParams.customerId;
                }]
            }
    })
};

不过,我只是要我,没有数据创建的templateUrl和URL没有得到的ID。

下面是我的命令控制器

 (函数(){
    使用严格的;
    角
        .module('app.orders')
        .controller('OrdersController',OrdersController);    功能OrdersController($ stateParams){
    的console.log('订单');
            VAR VM =这一点;
            vm.title =客户订单;    }
}());

这是我设置了订单的路线。我确信引用:标识的每一个接触ID

 (函数(){
    使用严格的;    角
        .module('app.orders')
        的.config(配置);    功能配置($ stateProvider){
        $ stateProvider
            .STATE('订单',{
                网址:'/订单:客户ID',
                templateUrl:./components/orders/orders.html',
                控制器:'OrdersController',
                controllerAs:CTRL
        })
        // $ locationProvider.html5Mode(假);
    };
})();


解决方案

第一,国家应该定义在 参数 。这可能是部分的 网​​址 params:一个{} (或两者),但在至少一些...

  // PARAMS
.STATE('客户',{
    网址:'/客户,
    PARAMS:{客户ID:空}
    ...// URL
.STATE('客户',{
    网址:'/用户/:客户ID',
    ...// URL和放大器; PARAMS
.STATE('客户',{
    网址:'/用户/:客户ID',
    PARAMS:{客户ID:1} //默认值
    ...

到这一点,我们可以创建UI的SREF是这样的:

  //而不是这个
< TD><一个UI的SREF =命令({} ctrl.cust.id)>查看订单< / A>< / TD>
//我们需要名CustomerID和它的值对象
< TD><一个UI的SREF =命令({客户ID:ctrl.cust.id})>查看订单< / A>< / TD>

有关详细信息,请检查该问答集答:

如何使用UI的SREF在UI的路由器控制器

传递参数

I am working through the ui-router docs. I have a decent feel for what I am trying to do with ui-sref. In the bottom td I am adding ui-sref to the desired href. I made sure to call the state that I want to fire and the brackets are the route Params that I am trying to create.

the problem is though that I am getting Syntax Error: Token '.' is at column {2} of the expression [{3}] starting at [{4}] from the Angular docs.

I made sure to reference some additional info incase I am missing in any part of my code.

 <div class="row" >

  <div class="panel panel-primary">

    <div class="panel-heading">
      Customer List
    </div>

    <div class="panel-body">
      Filter: <input type="text" ng-model="customerFilter">
    </div>

    <table class="table table-striped table-responsive">
      <tr>
        <th ng-click="ctrl.doSort('name')">Name</th>
        <th ng-click="ctrl.doSort('city')">City</th>
        <th ng-click="ctrl.doSort('orderTotal')">order total</th>
        <th ng-click="ctrl.doSort('joined')">joined</th>
        <th>&nbsp;</th>
      </tr>

      <tr data-ng-repeat = "cust in ctrl.customers |filter: customerFilter| orderBy:ctrl.sortBy:ctrl.reverse">
        <td>{{cust.name | uppercase}}</td>
        <td>{{cust.city}}</td>
        <td>{{cust.orderTotal | currency}}</td>
        <td>{{cust.joined |date}}</td>

        <td><a ui-sref="orders({ctrl.cust.id})">View Orders</a></td>
      </tr>

    </table>

  </div>
    <span>Total customers: {{ctrl.customers.length}}</span>

</div>

Here is the top part of my controller. I am working with controllerAs and trying to get more used to the John Papa style guide

angular
    .module('app.customers')
    .controller('CustomerController', CustomerController);

function CustomerController($stateParams) {
    var vm = this;
// customerId comes from url param
    console.log($stateParams);
    var customerId = $stateParams.customerId;
    vm.orders = null;

I am getting back an empty object for $stateParams

My route file is broken up as specific as I could make it. I created a view object, created a main view and referenced it in the html. I made a resolve object that will take the $stateParams

 angular
    .module('app.customers')
    .config(config);

function config($stateProvider) {
    console.log('customers route')
    $stateProvider
        .state('customers',{
            url:'/customers', 
            views: {
                "main@": {
                    templateUrl: './components/customers/customers.html',
                    controller: 'CustomerController',
                    controllerAs: 'ctrl'
                }
            },
            resolve: {
                customerId: ['$stateParams', function($stateParams) {
                    return $stateParams.customerId;
                }]
            }
    })
};

However, I am just going to the templateUrl I created with no data and the url is not getting the id.

Here is my orders controller

(function() {
    'use strict';
    angular
        .module('app.orders')
        .controller('OrdersController', OrdersController);

    function OrdersController($stateParams) {
    console.log('in orders');
            var vm = this;
            vm.title = "Customer Orders";

    }
}());

This is the route that I set up for orders. I made sure to reference :Id each contact id.

(function() {
    'use strict';

    angular
        .module('app.orders')
        .config(config);

    function config($stateProvider) {
        $stateProvider
            .state('orders',{
                url:'/orders:customerId', 
                templateUrl: './components/orders/orders.html',
                controller: 'OrdersController',
                controllerAs: 'ctrl'
        })
        // $locationProvider.html5Mode(false);
    };    
})();

解决方案

Firstly, state should define the parameter. It could be part of url or params : {} (or both), but at least some...

// params
.state('customers',{
    url:'/customers', 
    params: { customerId : null }
    ...

// url
.state('customers',{
    url:'/customers/:customerId', 
    ... 

// url & params 
.state('customers',{
    url:'/customers/:customerId', 
    params: { customerId : 1 } // default value
    ...

Having this, we can create ui-sref like this:

// instead of this
<td><a ui-sref="orders({ctrl.cust.id})">View Orders</a></td>
// we need object with name customerId and its value
<td><a ui-sref="orders({customerId: ctrl.cust.id})">View Orders</a></td>

For more details check this Q & A:

How to pass parameters using ui-sref in ui-router to controller

这篇关于在UI的SREF一个前pression错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆