在AngularJS应用程序中使用封装指令和路由的推荐方法是什么?(What is the recommended way to use an encapsulated directive and routing within an AngularJS app?)
我正在尝试使用AngularJs 1.5应用程序的最佳实践 。 已经关注了Todd Motto和John Papa的风格指南
问题是:我的应用程序使用ngRoute进行路由 。 所以这:
function RoutingConfig($routeProvider) { $routeProvider .when('/route1', { controller: 'Ctrl1 as vm', templateUrl: 'partials/route1.html', resolve: { myCoolService: myCoolService } // to abbreviate }) .when('/route2', { controller: 'Ctrl2 as vm', templateUrl: 'partials/route2.html' }) .otherwise({redirectTo: '/route1'}): } angular .module('myApp', ['ngRoute']) .config(RoutingConfig);
好。 到现在为止还挺好。
现在,让我们说,在我走向Angular 2的道路上, 我想采用指令方法封装所有内容 。 这样,我将使用AngularJS 1.5创建类似Web组件的东西。
如果我遵循正确的做法,我可以为我的route1创建一个指令,并直接为该指令定义一个控制器:
function MyCoolDirective() { var directiveDefObj = { controller: Ctrl1, controllerAs: 'vm', scope: { data: "=", }, bindToController: true, // isolated scope link: linkFnc // just declared to abbreviate }; return directiveDefObj; } angular .module('myApp') .directive('MyCoolDirective', MyCoolDirective);
那么, 我应该在哪里声明控制器? 在路由配置中? 在每个指令里面? 对我来说听起来有点冗余。
I'm trying to adopt best practices with an AngularJs 1.5 app. Already following Todd Motto's and John Papa's style guides
The thing is: my app has routes using ngRoute. So, this:
function RoutingConfig($routeProvider) { $routeProvider .when('/route1', { controller: 'Ctrl1 as vm', templateUrl: 'partials/route1.html', resolve: { myCoolService: myCoolService } // to abbreviate }) .when('/route2', { controller: 'Ctrl2 as vm', templateUrl: 'partials/route2.html' }) .otherwise({redirectTo: '/route1'}): } angular .module('myApp', ['ngRoute']) .config(RoutingConfig);
Ok. So far, so good.
Now, let's say that, in my road to Angular 2, I want to adopt the encapsulate everything in a directive approach. This way, I'll create something like web components, using AngularJS 1.5.
If I'm following the right practices, I could create a directive for my route1 and directly define a controller for that directive:
function MyCoolDirective() { var directiveDefObj = { controller: Ctrl1, controllerAs: 'vm', scope: { data: "=", }, bindToController: true, // isolated scope link: linkFnc // just declared to abbreviate }; return directiveDefObj; } angular .module('myApp') .directive('MyCoolDirective', MyCoolDirective);
So, where should I declare the Controllers? In the routing configuration? Inside each directive? Sounds a bit redundandt for me.
原文:https://stackoverflow.com/questions/37310516
最满意答案
这可以通过使用
dcast
的data.table
来实现,它可以使用多个value.var
library(data.table) dcast(setDT(data), id~rater, value.var=c("x", "y"), sep=".") # id x.1 x.2 x.3 y.1 y.2 y.3 #1: 1 0 1 2 0 1 2 #2: 2 0 1 2 0 1 2
This can be achieved by using
dcast
fromdata.table
which can take multiplevalue.var
library(data.table) dcast(setDT(data), id~rater, value.var=c("x", "y"), sep=".") # id x.1 x.2 x.3 y.1 y.2 y.3 #1: 1 0 1 2 0 1 2 #2: 2 0 1 2 0 1 2
相关问答
更多-
R:你能指定reshape / cast中变量列的顺序吗?(R: Can you specify the order of variable columns from reshape/cast?)[2022-09-07]
我没有看到原因,为什么列顺序应该重要。 但是,您可以随时更改顺序: master[,c(names(master)[1:3], as.character(unique(master_long$factor)))] coy_name gvkey_iid variable sales cogs_xdep sga 1 Coy_1 111111 X20130930 1 4 7 2 Coy_1 111111 X20130831 2 ... -
如果你只有两个位置,你可以把它们放在正则表达式中,说明它们可能在名称的开头或结尾: library(tidyverse) df_wide %>% gather(variable, value, -Month) %>% mutate(location = sub('.*(Cabo|Acapulco).*', '\\1', variable), variable = sub('_?(Cabo|Acapulco)_?', '', variable)) %>% ...
-
您可以将公式创建为字符串,然后使用as.formula() 在lhs我使用setdiff()抓取所有不是DATA或measurement的列名。 library(reshape2) lhs <- paste0(setdiff(names(DF), c("DATA", "measurement")), collapse = "+") dcast(DF, as.formula(paste0(lhs, "~ measurement")), fun.aggregate = mean, value.var = "D ...
-
这可以通过使用dcast的data.table来实现,它可以使用多个value.var library(data.table) dcast(setDT(data), id~rater, value.var=c("x", "y"), sep=".") # id x.1 x.2 x.3 y.1 y.2 y.3 #1: 1 0 1 2 0 1 2 #2: 2 0 1 2 0 1 2 This can be achieved by using dcast f ...
-
require(reshape2) mydat <- recast(test_data,hour~ride) mydat hour A B C 1 1 1 0 1 2 2 0 1 0 3 3 2 1 0 # 2nd part for(i in 2:ncol(mydat)){ for(ii in 1:nrow(mydat)){ if(mydat[ii,i] > 0) {mydat[ii,i] <- 1} } } hour A B C 1 1 1 0 1 ...
-
你可以使用tidyr spread library(dplyr) library(tidyr) mutate(df1, var=factor(items, levels=unique(items), labels=paste0('items', seq(n_distinct(items))))) %>% spread(var, items, fill='') # customer_code items1 items2 items3 #1 ...
-
你可以试试的东西: xmax <- max(table(x$conv)) xsplit <- split(x, x$conv) xsplit <- sapply(xsplit, function(tab){c(tab$name, rep(NA, xmax-length(tab$name)))}) x2 <- data.frame(conv=x$conv[!duplicated(x$conv)], t(xsplit), stringsAsFactors=F) colnames(x2)[-1]<-paste(" ...
-
这里是另一个选择,可以从data.table中melt ,这可以采用多种measure patterns 。 library(data.table) melt(setDT(toy_wide), measure = patterns("^colors", "^days"), value.name = c("colors", "days"), variable.name = "index")[order(first_key, second_key)] Here is another option wi ...
-
我可能不称职,但似乎你已经将你的变量命名为reshape所假定的变量(例如v.mandate而不是mission.v )。 我改变了他们的命名,并可以让它工作: mydf <- data.frame(district = c(1:2), mandate.v = c(1, 3), mandate.s = c(2, 4), perc.v = c(.4, .3), ...
-
在通过IDnum添加连续计数后,您可以使用基本函数reshape进行此IDnum 。 假设您的数据存储在名为df的data.frame : df2 <- within(df, count <- ave(rep(1,nrow(df)),df$IDnum,FUN=cumsum)) 提供名为“time”的连续计数的新列。 现在我们可以reshape为宽幅格式 reshape(df2,direction="wide",idvar="IDnum",timevar="count") IDnum zipcod ...