AngularJS数据绑定无法与外部控制器文件一起使用

时间:2017-08-30 18:26:33

标签: javascript angularjs angularjs-directive

我在Demo上有一个简单的代码文件 我正在尝试使用ng-bind=""ng-init,但两个人都没有工作。基本上我必须在{{1}}上显示一些数据。

2 个答案:

答案 0 :(得分:1)

你实际上错过了这个,

  <div ng-app="myApp.controllers" ng-controller="Report as ctrl">

<强>样本

&#13;
&#13;
angular.module("myApp.controllers", []);

(function(angular) {
  "use strict";
  function Report($scope) {
    let ctrl = this;
    ctrl.reports = "Rolys Royc H87H";
    $scope.reportInfo = "Aircraft";

    ctrl.$onInit = function() {
      $scope.reportInfo = "Aircraft";
    };
  }
  Report.$inject = ["$scope"];

  angular.module("myApp.controllers").controller("Report", Report);
})(angular);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html>

<head>
  <title>AngularJS Directives</title>
</head>
<body>
  <h1>Sample Application</h1>
  <div ng-app="myApp.controllers" ng-controller="Report as ctrl">
    <h4 ng-bind="ctrl.reports"></h4>
    <h4 ng-bind="reportInfo"></h4>
  </div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这很有效。您尚未指定ng-appng-controller

Codepen Demo

代码:

<html>

<head>
  <title>AngularJS Directives</title>
</head>

<body>
  <h1>Sample Application</h1>
  <div ng-app="myApp.controllers" ng-controller="Report as ctrl">

    <h4 ng-bind="ctrl.reports"></h4>
    <h4 ng-bind="reportInfo"></h4>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script src="a.js"></script>
  <script src="abc.js"></script>
</body>

</html>