首页 \ 问答 \ 在AngularJS应用程序中使用封装指令和路由的推荐方法是什么?(What is the recommended way to use an encapsulated directive and routing within an AngularJS app?)

在AngularJS应用程序中使用封装指令和路由的推荐方法是什么?(What is the recommended way to use an encapsulated directive and routing within an AngularJS app?)

我正在尝试使用AngularJs 1.5应用程序的最佳实践 。 已经关注了Todd MottoJohn 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
更新时间:2024-03-26 11:03

最满意答案

这可以通过使用dcastdata.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 from data.table which can take multiple 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

相关问答

更多

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果