无法在ui-view中运行JavaScript配置

时间:2016-06-10 01:26:27

标签: javascript html angularjs angular-ui-router

我有一个演示用Angular UI RouterHere

app.js:

angular.module('app', ['ui.router'])
.config(function( $stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');

    $stateProvider
    .state('main', {
        url: '',
        template: '<div ui-view></div>',
    })
    .state('main.home', {
        url: '/home',
    templateUrl: 'home.html',
    controller: 'HomeCtrl',
    controllerAs: 'home'
  });
})
.controller('HomeCtrl', ['$scope', function ($scope) {
  console.log('home');
}]);

的index.html

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <title>UI Router</title>

  </head>

  <body ng-app="app">
    <a ui-sref=".main.home" href="javasacript:void(0)">Show Agoda</a>
    <div ui-view=""></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
    <script src="app.js"></script>

  </body>
</html>

home.html的

<div>
  Home Page
</div>

// This is code I need to work
<script type="text/javascript">
  agoda_ad_client = "1650121_5092";
  agoda_ad_width = 200;
  agoda_ad_height = 200;
</script>
<script type="text/javascript" src="//banner.agoda.com/js/show_ads.js"></script>

我想点击Show Agoda链接时,会转到主页并显示Angoda Banner。但它不起作用。

我在没有ui-view的情况下使用时效果很好:

的index.html

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <title>UI Router</title>

  </head>

  <body ng-app="app">
    <a ui-sref=".main.home" href="javasacript:void(0)">Show Agoda</a>
    <div ui-view=""></div>
    // This code work fine
    <script type="text/javascript">
      agoda_ad_client = "1650121_5092";
      agoda_ad_width = 200;
      agoda_ad_height = 200;
    </script>
    <script type="text/javascript" src="//banner.agoda.com/js/show_ads.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
    <script src="app.js"></script>

  </body>
</html>

如何在Agoda codeui-view)中使用home.html。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以通过以下链接尝试您的代码: <a ui-sref=".main.home" href="javasacript:void(0)">Show Agoda</a>将其替换为<a ui-sref="main.home" href="javasacript:void(0)">Show Agoda</a>

同时将<div ui-view=""></div>替换为<div ui-view></div>

希望这会对你有所帮助。