使用ng-view并按需加载控制器

时间:2015-07-16 17:26:47

标签: javascript angularjs ng-view

我正在使用ng-view尝试重用常用页面元素。也许我错误地理解了观点,这在这里是不合适的。

基本上,我有一个看起来像这样的html页面:

<html>
    <head>
        <!-- css and js here including index.js -->
    </head>
    <body ng-app="app" ng-controller="ctrl">
        <navigation-bar/>
        <div ng-view />
    </body>
</html>

这是我的index.js的样子

(function () {
    angular.module('app', ['ngRoute', 'ngSanitize'], function ($routeProvider) {});
    angular.module('app').controller("ctrl", function ($scope, $log, $q, $timeout) {});
    angular.module('app').config(["$routeProvider", function ($routeProvider) {
                $routeProvider.when("/management", {
                    templateUrl : "/experiment/management.html",
                    // Is there a way to load /experiment/managementController.js here?
                }
                })
            ]);
    })();

我的management.html

<div ng-controller="managementController">
<!-- A bunch of code -->
</div>

和managementController.js(我想在加载路径时才运行)

(function () {
    angular.module('app').controller("managementController", [
            "$scope", "$sce", "$log", function ($scope, $sce, $log) {
                // controller specific code
            }
        ]);
})();

为什么我要这样做?因为我有许多潜在的控制器有许多潜在的观点。 (大约50)

这不可能或不是ng-view的意图吗?

如果不是ng-view的意图,在这种情况下是否有什么可以帮助我?

2 个答案:

答案 0 :(得分:1)

文档中的

Take a look at the first example。查看script.js选项卡。

void Main()
{
    const string hardCodedConfigFilePath = @"\\ai-vmdc1\RedirectedFolders\jlambert\Documents\MyApp.exe.config";
    string xmlDocumentText = File.ReadAllText(hardCodedConfigFilePath);
    XmlDocument doc = new XmlDocument();
    //doc.Schemas.Add(, xsdPath
    //Console.WriteLine(xmlDocumentText);
    doc.LoadXml(xmlDocumentText);
    XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement["StationsSection"]);
    string firstStationConfiguration = doc.DocumentElement["StationsSection"].ChildNodes[0].OuterXml;//.InnerXml; //here's the chunk that contains my data
    XmlSerializer ser = new XmlSerializer(typeof(Stations));
    //  Console.WriteLine(xmlDocumentText);
    //object obj = ser.Deserialize(reader);
    Console.WriteLine(firstStationConfiguration);
    using (StringReader stringReader = new StringReader(firstStationConfiguration))
    {
        Stations sc = (Stations)ser.Deserialize(stringReader);
        Console.WriteLine(sc.Comment);
        Console.WriteLine(sc.FtpUsername);
        Console.WriteLine(sc.FtpPassword);
        Console.WriteLine(sc.DestinationFolderPath);
    }
}

// Define other methods and classes here
[Serializable]
public class Stations
{
    [XmlAttribute("Comment")]
    public string Comment { get; set;}
    [XmlAttribute]
    public string FtpUsername { get; set;}
    [XmlAttribute]
    public string FtpPassword { get; set;}
    [XmlAttribute]
    public string DestinationFolderPath { get; set;}
}

如果您的意思是要加载实际文件take a look at this post

答案 1 :(得分:1)

您可以在resolve中使用angular Js属性,该属性可以处理动态加载包含目标控制器的脚本,并在加载完成后解析承诺。

例如:

$routeProvider
    .when('/management',
        {
            templateUrl: '/experiment/management.html',
            resolve: resolveController('/app/controllers/managementController.js')
        });