javascript - 使用routeParams时,在一个页面上使用多个 Controller ?

标签 javascript angularjs html

我在代码中使用routeParams 时遇到问题。

第一个版本(工作正常)

索引.html

<div ng-controller="NaomiController">
      <my-customer></my-customer>
    </div>
    <hr>
    <div ng-controller="IgorController">
      <my-customer></my-customer>
    </div>

我的controller.js是:

    angular.module('docsScopeProblemExample', [])
    .controller('NaomiController', ['$scope', function($scope) {
      $scope.customer = {
        name: 'Naomi',
        address: '1600 Amphitheatre'
id:'na'
      };
    }])
    .controller('IgorController', ['$scope', function($scope) {
      $scope.customer = {

        name: 'Igor',
        address: '123 Somewhere'
id:'ig'
      };
    }])
    .directive('myCustomer', function() {
      return {
        restrict: 'E',
        templateUrl: 'my-customer.html'
      };
    });

和我的

my-customer.html 是:

Name:<a href="{{customer.id}}">{{customer.name}}</a> , Address: {{customer.address}}

之后我想在下一页使用routeParams..

代码的第二个版本(不起作用)

所以index.html是:

<div ng-view></div>

app.js 是:

var App = angular.module('App', [
  'ngRoute',
  'myControllers'
]);

App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        template: 'home.html',
        controller: 'IgorController','NaomiController'
      }).
      when('/home/:id', {
        templateUrl: 'partials/detail.html',
        controller: 'detailCtrl'
      }).
      otherwise({
        redirectTo: '/home'
      });
  }]);

我的controller.js是:

    myControllers.controller('NaomiController', ['$scope', function($scope) {
              $scope.customer = {
                name: 'Naomi',
                address: '1600 Amphitheatre'
        id:'na'
              };
            }])
            .controller('IgorController', ['$scope', function($scope) {
              $scope.customer = {

                name: 'Igor',
                address: '123 Somewhere'
        id:'ig'
              };
            }])
            .directive('myCustomer', function() {
              return {
                restrict: 'E',
                templateUrl: 'my-customer.html'
              };
            }).controller('detailCtrl',['$scope', function($scope){
             $scope.message: "Hello"
}])

其中 home.html 包含:

<div ng-controller="NaomiController">
          <my-customer></my-customer>
        </div>
        <hr>
        <div ng-controller="IgorController">
          <my-customer></my-customer>
        </div>

和detail.html有:

{{message}}

我的代码的第二个版本无法正常工作

最佳答案

您只能对 $routeProvider 使用单个 Controller

您可以做的是为您的 View 使用单个 Controller ,并将 View HTML 本身中的 2 个 Controller 与 ng-controller 关联起来

关于javascript - 使用routeParams时,在一个页面上使用多个 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762151/

相关文章:

javascript - Angular - 修改 $http 调用中的 Controller 变量

angularjs - 为什么即使我在 JSFiddle 中声明 ng-app ="MyApp"也需要 angular.bootstrap

javascript - 使用 Javascript 将表格行添加到表格中间

javascript - 显示重叠元素的 jQuery UI 菜单

javascript - 渲染到表中的 1,000 行速度很慢

javascript - Angular 服务参数错误

html - 希伯来语在html中是问号

html - 当悬停时填充扩展时,div 向下移动

javascript - 在 JQuery 回调中获取元素

javascript - onclick后禁用功能