簡體   English   中英

使用Angularjs和ui.router的單頁應用程序

[英]Single page application using Angularjs and ui.router

我正在嘗試使用Angularjs和ui.router創建一個單頁應用程序,但是我的代碼無法正常工作。

index.html:

<html>

  <head>
    <script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.12/angular.js"></script>
    <script src="uirouter.js"></script>
    <script src="route.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div  ui-view></div>
    <script src="NABHController.js"></script>
     <script src="nabh1Controller.js"></script>
      <script src="nabh2Controller.js"></script>
  </body>

</html>

route.js:

var compliance=angular.module('ikomplianzNABH',['ui.router']);
compliance.run(function($rootScope, $state) {
      $rootScope.$state = $state;
    });
compliance.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
    .state('/', { /*....This state defines All type of user login...*/
        url: '/',
        templateUrl: 'NABH.html',
        controller: 'NABHController'
    })
    .state('.nabh1', { /*....This state defines All type of user login...*/
        url: '/nabh1',
        templateUrl: 'nabh1.html',
        controller: 'nabh1Controller'
    })
    .state('.nabh2', { /*....This state defines All type of user login...*/
        url: '/nabh2',
        templateUrl: 'nabh2.html',
        controller: 'nabh2Controller'
    })
});

在此,當用戶鍵入URL http://localhost/test/ ,應呈現NABH.html頁面。 在此文件中,有一個鏈接可以路由到另一頁,但是此頁應在該NABH.html ui-view而不是索引頁內呈現。

NABH.html:

<div> 
<div ui-view>
<div class="clecklist">
<a ui-sref=".nabh2">Click Me</a>
</div>
</div>

</div>

當用戶單擊“ Click Menabh2.html應該呈現到此頁面中。

nabh2.html:

h1>{{msg}}</h1>

就我而言,什么都沒有發生。 在這里,只有我需要將NABH.html設置為父視圖nabh1.html,nabh2.html應將nabh1.html,nabh2.html都用作其子視圖。 我的完整Plunkr代碼在這里。

只是一個替代建議:另一個選擇是完全使用ng-include

Index.html:

<!DOCTYPE html>
<html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="controller.js"></script>
  </head>

  <body>
    <div ng-app="myApp" ng-controller="nabhController">
      <div>
        <label>{{message}}</label>
      </div>

      <div ng-view></div>
    </div>
  </body>

</html>

NABH.html:

<div ng-controller="nabhController"> 
  <div class="checklist">
    <div>
       <button ng-click="showNabh =! showNabh">Click Me</button>
     </div>

     <div ng-show="showNabh">
       <div ng-include="'nabh1.html'"></div>
     </div>
     <div ng-show="!showNabh">
       <div ng-include="'nabh2.html'"></div>
     </div>
  </div>
</div>

NABH1.html

<div ng-controller="nabh1Controller">
  {{message}}
</div>

NABH2.html

<div ng-controller="nabh2Controller">
  <h1>{{message}}</h1>
</div>

控制者

var app = angular.module('myApp', []);
app.controller('nabhController', function($scope) {
  $scope.message = "Welcome";
  $scope.showNabh = false;
});

app.controller('nabh1Controller', function($scope){
  $scope.message = "Nabh1"
});

app.controller('nabh2Controller', function($scope){
  $scope.message = "Nabh2"
});

app.config(function($routeProvider) {
    $routeProvider
      .when("/", {
        templateUrl: 'nabh.html',
        controller: 'nabhController'
      })
      .otherwise({
        redirectTo: '/'
      });
});

柱塞

您可以通過以下兩種方式之一進行操作:

第一種方式:

var compliance=angular.module('ikomplianzNABH',['ui.router', 'ngSanitize']);
compliance.run(function($rootScope, $state) {
      $rootScope.$state = $state;
    });
compliance.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
    .state('/', { /*....This state defines All type of user login...*/
        url: '/',
        templateUrl: 'NABH.html',
        controller: 'NABHController'
    })
    .state('.nabh1', { /*....This state defines All type of user login...*/
        url: '/nabh1',
        templateUrl: 'nabh1.html',
        controller: 'nabh1Controller'
    })
    .state('.nabh2', { /*....This state defines All type of user login...*/
        url: '/nabh2',
        templateUrl: 'nabh2.html',
        controller: 'nabh2Controller'
    })
});


var app = angular.module('myApp', []);
app.controller('nabhController', function($scope) {
  $scope.$sce = $sce;
  $scope.message = "Welcome";
  $scope.showNabh = false;
});

    app.controller('nabh1Controller', function($scope){
      $scope.message = "Nabh1"
    });

    app.controller('nabh2Controller', function($scope, $sce){
      $scope.message = "Nabh2"
    });

    app.config(function($routeProvider) {
        $routeProvider
          .when("/", {
            templateUrl: 'nabh.html',
            controller: 'nabhController'
          })
          .otherwise({
            redirectTo: '/'
          });
    });

<div> 
<div ui-view>
<div class="clecklist">
<a ng-bind-html="$sce.trustAsHtml(.nabh2.html)">Click Me</a>
</div>
</div>

</div>

第二種方式

 <div> 
    <div ui-view>
    <div class="clecklist">
    <a ng-click="TakeMeToLink()">Click Me</a>
    </div>
    </div>

var app = angular.module('myApp', []);
app.controller('nabhController', function($scope, $location) {
  $scope.$sce = $sce;
  $scope.message = "Welcome";
  $scope.showNabh = false;

 $scope.TakeMeToLinh = function() {
       $location.path("/nabh2");
    };
});

    app.controller('nabh1Controller', function($scope){
      $scope.message = "Nabh1"
    });

    app.controller('nabh2Controller', function($scope, $sce){
      $scope.message = "Nabh2"
    });

    app.config(function($routeProvider) {
        $routeProvider
          .when("/", {
            templateUrl: 'nabh.html',
            controller: 'nabhController'
          })
          .otherwise({
            redirectTo: '/'
          });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM