2

我是 Angular 的新手,并且已经阅读了所有教程,但我正处于学习曲线的陡峭点,我第一次构建真正的应用程序。

我只想在单击特定单选按钮时显示 div。

这是我的代码:

// HTML file
<label class="checkbox-inline">
  <input type="radio" name="ast-adh-p1-radio" ng-model="value" ng-change='answerP1(value)' value="yes"> Yes
</label>
<label class="checkbox-inline">
  <input type="radio" name="ast-adh-p1-radio" ng-model="value" ng-change='answerP1(value)' value="no"> No
</label>
 <div ng-show="showQ1A">hello world </div>

// Controller file
 angular
   .module('adh.controllers', ['ngModel'])
  .controller('adhCtrl', ['$scope', function ($scope) {
    $scope.answerP1 = function(value) {
      console.log('clicked radio button', value);
      // change $scope.showQ1A accordingly
    }
}]);

不幸的是,我收到以下错误,并且无法通过谷歌搜索解决:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module adh.controllers 
due to:
Error: [$injector:modulerr] Failed to instantiate module ngModel due to:
Error: [$injector:nomod] Module 'ngModel' is not available! You either misspelled 
the module name or forgot to load it. If registering a module ensure that you 
specify the dependencies as the second argument.

我需要加载外部文件才能使用ngModel吗?我在文档中看不到这一点。

如果是这样,有没有更简单的方法来做事?

4

1 回答 1

6

没有调用模块ngModel,删除该依赖项

 angular
   .module('adh.controllers',[])
  .controller('adhCtrl', ['$scope', function ($scope) {
    $scope.answerP1 = function(value) {
      console.log('clicked radio button', value);
      // change $scope.showQ1A accordingly
    }
}]);

ngModel是一个指令。

于 2013-11-12T15:21:37.323 回答