用angularjs ui路由器替换另一部分自我部分
在客户视图中,我有一个创建客户按钮,该按钮应该加载customers.html所在的customers.create.html局部视图.
如何将当前视图“customers.html”替换为另一个视图“customers.create.html”? customers.html客户 <div> <button ng-click="create()" class="btn btn-default" ui-sref="customers.create">Create</button> </div> <div style="height:500px;" ng-grid="myOptions"></div> app.js $stateProvider .state('customers',{ url: "/customers",templateUrl: "../views/customers.html",controller: ['$scope','$stateParams','$state',function ( $scope,$stateParams,$state){ }] }) .state('customers.create',{ url: "/create",templateUrl: "../views/customers.create.html" }) 在单击创建按钮时,路径将更改为/ customers / create,而不是html视图,它将保持不变. 我不能把< div ui-view>< / div>在创建按钮下,因为客户数据网格和创建按钮仍然可见. 也许我不应该使用分层客户.创建视图? 解决方法
我们需要使用绝对视图名称,以准确地通知ui-router,在哪里(重新)放置子模板. (查看这个
example)在这里阅读更多:
> View Names – Relative vs. Absolute Names 引用: // absolutely targets the unnamed view in root unnamed state. // <div ui-view/> within index.html "@" : { } 因此,根视图名称是空字符串,对于子项,可以表示为“@” $stateProvider .state('customers',{ url: "/customers",controller: ['$scope',function($scope,$state) { }] }) .state('customers.create',{ url: "/create",views: { '@': { templateUrl: "../views/customers.create.html" } } }) 请参阅本plunker中的更多内容 延伸.任何状态定义都是定义视图名称,其template / templateUrl / templateProvider属于该名称.如果只有一个模板要注入父ui-view =“”(未命名),我们可以使用以下语法: .state('customers',templateUrl: "tpl.customers.html",controller: .... }) 这等于这个语法: .state('customers',views: { // explicit information that we target unnamed view '': { templateUrl: "tpl.customers.html",controller: ... } } }) 所以,如果我们必须在根级别上查看目标 <h4 data-ui-view="header"></h4> <div data-ui-view=""></div> 我们可以定义这样的状态: $stateProvider .state('customers',views: { 'header': { template: '<div>customers</div>',// controller... },'': { templateUrl: "tpl.customers.html",controller: ... } } }) .state('customers.create',views: { 'header@': { template: "<div>create</div>",},'@': { templateUrl: "tpl.customers.create.html",} } }) ; 参见扩展示例plunker EXTEND:给出评论的答案:
如上所述:View Names – Relative vs. Absolute Names:
那会发生什么? >放在index.html中的ui-view =“”获得绝对名称“@”.它由三部分组成(只有一个字符).分隔符是@,它左边的字符代表viewName,右边的字符代表stateName 它可以使用以下其中一种: views : { "@customers" : { template: .... } } // or template: .... 因为第二个只使用隐式表示法,最终会以“”(无名字)“@”(分隔符)“customers”(parent stateName)==“@customers”结尾 4.我们可以使用相同的表示法来定位根(index.html). 根名称是“”,而视图名称是“”,我们最终在“”“@”“”==“@”.这就是为什么这个神奇的环境能够将我们的视图放到root ui-view =“”index.html来自“@” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |