论证' myCtrl'不是一个函数,未定义是持久的

时间:2016-09-26 01:40:13

标签: angularjs

有很多答案,但不是很重要,所以我想再问一遍。我试图使用我找到的Angular代码的和平:

<!DOCTYPE html>
<html ng-app="" ng-controller="myCtrl">
    <body>
    <h3>Select some text </h3>
    <div ng-mouseup="showSelectedText()">
        Do you think that facebook, twitter, linkedin are designed to make people addict ?, Then you have to read this article. The most popular social networking sites are following people's habit. It is not concept, it is people's habit
    </div>
    <br/>
    <h3>Selected Text</h3>
    <div ng-bind="selectedText"></div>
    <script>
        function myCtrl($scope) {

            $scope.showSelectedText = function() {
                $scope.selectedText =  $scope.getSelectionText();
            };

            $scope.getSelectionText = function() {
              var text = "";
              if (window.getSelection) {
                  text = window.getSelection().toString();
              } else if (window.getSelection && window.getSelection.type != "Control") {
                  text = document.selection.createRange().text;
              }
              return text;
            };
        }
    </script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
    </body>
</html>

由于某种原因,它不断给我一个错误:

Argument 'myCtrl' is not a function, got undefined

尝试更改为ng-app="myApp"时,我收到此错误:

Error: $injector:modulerr
Module Error

据我所知,使用新的角度版本可以围绕使用ng-controller的语法进行一些迁移,但我无法弄清楚我应该做什么

提前谢谢! 阿米尔

1 个答案:

答案 0 :(得分:1)

在您的html ng-app="myApp"中添加此内容,这应该是您的脚本。

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', myCtrl);
function myCtrl($scope) {

$scope.showSelectedText = function() {
    $scope.selectedText =  $scope.getSelectionText();
};

$scope.getSelectionText = function() {
  var text = "";
  if (window.getSelection) {
      text = window.getSelection().toString();
  } else if (window.getSelection && window.getSelection.type != "Control") {
      text = document.selection.createRange().text;
  }
  return text;
};
}
</script>

在自定义脚本

之前加载angular.min.js

工作人员:http://plnkr.co/edit/2Vb8nCYVhI9vMdHvn3Yf?p=preview