首页 \ 问答 \ 使用AngularUI路由器动态加载控制器(Using the AngularUI Router to dynamically load controllers)

使用AngularUI路由器动态加载控制器(Using the AngularUI Router to dynamically load controllers)

我在一个拥有数十个模板的应用程序中使用ui-router 。 每个模板都有一个控制器。

从我一直在读的东西,这样的东西(设置路线)应该工作:

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('start', {
      url: '/start',
      templateUrl: 'partials/start.html',
      controller: 'StartCtrl'
    })
});

这是假设StartCtrl之前已定义。 该应用程序最终将拥有数十个控制器,并且不希望一次性下载所有控制器的开销。 如何仅在请求模板时加载控制器?


I'm using ui-router in an app that will have dozens of templates. Each template will have a controller.

From what I've been reading, something like this (to setup a route) ought to work:

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('start', {
      url: '/start',
      templateUrl: 'partials/start.html',
      controller: 'StartCtrl'
    })
});

That's assuming StartCtrl was previously defined. The app will eventually have dozens of controllers, and don't want the overhead of downloading all of them at once. How can I load a controller only when the template is requested?


原文:https://stackoverflow.com/questions/21247327
更新时间:2023-07-08 12:07

最满意答案

您需要将适配器更改为以下内容:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, products){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
             convertView = View.inflate(getBaseContext(), android.R.layout.simple_list_item_2, null);

        ((TextView) convertView.findViewById(android.R.id.text1)).setText(products[position]);
        ((TextView) convertView.findViewById(android.R.id.text2)).setText(definitions[position]);

        return convertView;
    }
};

You need to change your adapter to something like this:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, products){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
             convertView = View.inflate(getBaseContext(), android.R.layout.simple_list_item_2, null);

        ((TextView) convertView.findViewById(android.R.id.text1)).setText(products[position]);
        ((TextView) convertView.findViewById(android.R.id.text2)).setText(definitions[position]);

        return convertView;
    }
};

相关问答

更多
  • 您需要将一个可用主题应用于您的应用程序。 你可以做AndroidManifest.xml ,只需使用android:theme属性: android:theme="@android:style/Theme.Holo"如果你想要黑暗的主题或android:theme="@android:style/Theme.Holo.Light"如果你想要轻主题。 如果您将此应用程序放在一个标记中,则只会对相应的活动进行样式设置,如果将其放在标记中,则样式将应用于整个应用程序。 ...
  • 您需要将适配器更改为以下内容: adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, products){ @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) convertView = Vi ...
  • 首先,请确保您的应用清单文件具有网络访问权限。 如果您使用模拟器,请使用您的计算机IP地址而不是localhost 。 我建议你阅读本教程 Firstly be sure that you have network access permission on your app manifest file.
    产品风味的工作方式就是让每个dimension的数字风格乘以buildTypes的数量,在您的情况下,您在一个维度中有3个风格,并且2个构建类型以3x2 = 6构建结束变体如下: qaDebug qaRelease prodDebug prodRelease devDebug devRelease 每个组合都是相关产品风格配置和相关构建类型配置组合的实际组合。 对于变体qaDebug含义,它的配置来自定义的qa和定义下的debug组合。 因此,如果debug已经定义了proguardFiles ,通常不需要 ...
  • 您的广告ID应以ca-app-pub-your_key开头,其中包含ID,横幅ID以及插页式ID ID 所以使用这些: ca-app-pub-4526809957646802/687614978 ca-app-pub-4526809957646802/6876146978 Your ad ids should start wit ...
  • 来自你的logcat 11-22 12:10:56.958: E/AndroidRuntime(381): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.assent.app/com.example.assent.app.AndroidJSONParsingActivity}: android.os.NetworkOnMainThreadException 你正在主线程上下载你 ...
  • 既然你是初学者我建议你学习如何使用框架使用框架有很多好处如果不是你将需要实现一些复杂的代码来处理,如缓存,调整大小,避免内存崩溃,处理几乎所有你面临的API等等 对于图像下载我建议 毕加索 (我建议最多) 滑行 对于普通的Http请求 齐射 你也可以让我们Volley下载图片。 当你开始学习时,它很难理解,但在你真正理解之后,我保证你不会使用常规HTTPURLConnection来处理任何HTTP请求 如果您需要上传进度,请告诉我我也会添加代码,尽管它很复杂。 编辑: 到目前为止,我已经测试了你的代码,它完 ...
  • 在你的xml中, android:onClick="onClick1 (sparkMain)" 导致问题。 请更换 android:onClick="onClick1 (sparkMain)" 至 android:onClick="onClick1" 它将解决问题。 In your xml, android:onClick="onClick1 (sparkMain)" cause the problem. Please change android:onClick="onClick1 (sparkM ...
  • 我发现的唯一方法是将物品设置为最低价格并实际购买它。 完成测试后,您可以退款。 如果您在第一个小时左右退款,则无论如何都不会从您的卡中扣款。 The only way I found was to set the item to the minimum price and actually buy it for real. When you have finished testing, you can refund it. If you refund within the first hour or so, ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)