简体   繁体   English

如何使ui-router控制器正常工作?

[英]How can I make ui-router controller to work?

I've set up two simple URLs, with simple different templates and the same controller, but it doesn't work 我已经设置了两个简单的URL,它们具有简单的不同模板和相同的控制器,但是不起作用

HEAD: 头:

<script src="jsLib/angular_v1.4.js" type="text/javascript"></script>
<script src="jsLib/angular-ui-router.min.js" type="text/javascript"></script>
<script src="routes.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>

HTML: HTML:

<body ng-controller="MainCTRL as ctrl">
{{ctrl.nameApp}}
<div ui-view></div>

app.js: app.js:

angular
    .module("app", ["ui.router"])
    .controller("MainCTRL", MainCTRL)
    .config(configA);


function MainCTRL($location){
    this.nameApp = "nameApp";
}

routes.js: routes.js:

function configA($urlRouterProvider, $stateProvider){

    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state("/",{
           url: "/",
           templateUrl : "/testingBlock.htm",
           controller : "MainCTRL"
        })
        .state("login",{
            url: "/login",
            templateUrl : "/app/templates/login.htm",
            controller : "MainCTRL"
        });

}

login.htm: login.htm:

<div>Header</div>

<div>Main</div>

<div>Footer</div>

{{name.nameApp}}

testingBlock.htm: testingBlock.htm:

 <h2>Hello goHenry</h2>

{{MainCTRL.nameApp}}

It doesn't display MainCTRL.nameApp 它不显示MainCTRL.nameApp

Wanted to show you where is the issue, but did not found any. 想要告诉您问题出在哪里,但没有发现任何问题。 But then I realized that the issue is inside of the state views. 但是后来我意识到问题出在状态视图之内。

There is a working version of your code . 您的代码一个有效的版本

All adjustments are really very small (not important). 所有的调整实际上都很小(不重要)。 The index.html index.html

<html ng-app="app" >

  <head>
    <title>my app</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"
    ></script>
    <script src="//rawgit.com/angular-ui/ui-router/0.2.15/release/angular-ui-router.js"
    ></script>
    <script src="routes.js"></script>
    <script src="app.js"></script>
  </head> 

  <body ng-controller="MainCTRL as ctrl">
    {{ctrl.nameApp}}

    <br><a href="#/">#/</a> - default 
    <br><a href="#/login">login</a>

    <hr><div ui-view=""></div>

  </body> 
</html>

The big one is here "controllerAs" 这里最大的是"controllerAs"

$stateProvider
    .state("/",{
       url: "/", 
       templateUrl : "testingBlock.htm",
       controller : "MainCTRL",
       controllerAs:"MainCTRL",
    })
    .state("login",{
        url: "/login",
        templateUrl : "app/templates/login.htm",
        controller : "MainCTRL",
        controllerAs:"name",
    });

So, what is different? 那么,有什么不同呢? to support these statements: 支持以下声明:

  • {{name.nameApp}} in the "testingBlock.htm", “ testingBlock.htm”中的{{name.nameApp}}
  • {{MainCTRL.nameApp}} in the "app/templates/login.htm", {{MainCTRL.nameApp}} app / templates / login.htm“中的{{MainCTRL.nameApp}}

We need to use controllerAs and name as name or MainCTRL (compare your code and this example ) 我们需要使用controllerAs和name作为name或MainCTRL(比较您的代码和此示例

NOTE: do not mix ng-controller and UI-Router states controllers. 注意:请勿混用ng-controllerUI-Router 状态控制器。 These should be different objects, because UI-Router can use other resolve then the angular native ng-controller . 这些应该是不同的对象,因为UI-Router可以使用其他解析方式,而不是angular native ng-controller Later it could bring surprises... 后来可能会带来惊喜...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ui-router测试依赖于服务的AngularJS控制器? - How can I test an AngularJS controller that relies on a service, using ui-router? 如何访问ui路由器的UrlMatcher? - How can i access ui-router's UrlMatcher? Ui-router加载状态,那么我该如何运行一个函数呢? - Ui-router loads state, then how can I run a function? 如何使UI-Router打开选项卡 - How to make UI-Router open a tab 如何在ng-repeat中将项目路由到angularjs UI-Router中的其他URL,需要在ui-router中修改状态 - How can I route to different URLS in angularjs UI-Router for items in ng-repeat, Need to modify state in ui-router AngularJs UI路由器控制器 - AngularJs ui-router controller 当状态发生变化时,如何使角度ui-router的父状态始终执行控制器代码? - How to make angular ui-router's parent state always execute controller code when state changes? UI路由器无法将已解析的对象注入控制器 - UI-router can't inject resolved object into controller 如何使用AngularJs,Ui-Router和MongoDB轮询数据库并填充控制器? - How do I poll a database and populate a controller using AngularJs, Ui-Router, and MongoDB? 如何将Angular-Chart.JS数据添加到UI-Router .State Controller中? - How do I add Angular-Chart.JS data into UI-Router .State Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM