【问题标题】:Angular Binding In Partials部分角度绑定
【发布时间】:2013-09-18 13:19:15
【问题描述】:

我正在使用 Angular 种子(从 Angular 网页下载)并尝试在部分 . 这是代码:

app.js

'use strict';
// Declare app level module which depends on filters, and services
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives',      'myApp.controllers']).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller:   'mainPage'});
  $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
   $routeProvider.otherwise({redirectTo: '/view1'});

   }]);

controllers.js

'use strict';

 /* Controllers */

angular.module('myApp.controllers', []).
controller('mainPage', ['$scope',function() {

  $scope.data= "this is my data";
}])
  .controller('MyCtrl2', [function() {

 }]);

index.html

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body>
<ul class="menu">
<li><a href="#/view1">view1</a></li>
<li><a href="#/view2">view2</a></li>
</ul>

<div ng-view></div>
... some more angular includes etc

partial1.html

<p>This is the partial for view 1. {{data}}</p>

我期待看到控制器中定义的数据,但我只是看到: "这是视图 1 的部分内容。{{data}}"

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我认为主要问题是您没有将 $scope 传递给控制器​​函数。您应该在浏览器控制台上收到错误消息。在此处查看正在工作的 Plunker:

    http://plnkr.co/edit/ItziGeIVI5a9L56P1UCx

    var myApp = angular.module('myApp', []).
      config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/view1', {templateUrl: 'partial1.html', controller:   'mainPage'});
      $routeProvider.when('/view2', {templateUrl: 'partial2.html', controller: 'MyCtrl2'});
       $routeProvider.otherwise({redirectTo: '/view1'});
    
       }]);
    
    myApp.controller('mainPage', ['$scope',function($scope) {
      $scope.data = "this is my data";
    }])
    .controller('MyCtrl2', ['$scope', function($scope) {
      $scope.data = "this is my data 2";
    }]);
    

    【讨论】:

      【解决方案2】:

      这个:

      controller('mainPage', ['$scope', function() {
        $scope.data= "this is my data";
      }])
      

      应该是:

      controller('mainPage', ['$scope', function($scope /* ADD THIS */) {
        $scope.data= "this is my data";
      }])
      

      【讨论】:

        猜你喜欢
        • 2015-10-11
        • 1970-01-01
        • 2019-03-31
        • 2016-05-26
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2020-01-03
        相关资源
        最近更新 更多