AngularJS控制器不起作用

时间:2017-06-14 11:22:37

标签: javascript angularjs model-view-controller

我刚开始学习mvc并认为angular是一个很好的开始。首先,我只想知道我是否可以设置我的控制器工作。但我有问题。 要简短我的第一篇文章,这里是代码: index.php

<!DOCTYPE html>
<html>

<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js">
    </script>
    <title>afa</title>
    <link rel="stylesheet" type="text/css" href="stil-index.css">
</head>

<body ng-app="myapp">

    <div class="flex-container" ng-controller="mainController">
        <header>
            <h1>{{pages.name}} </h1>
        </header>

        <article class="article">
            <h1><?php echo "afgaga?" ?></h1>
            <p>
                <?php echo "agasdga" ?>
            </p>
            <p><strong><?php echo "agsadga" ?></strong>
            </p>
        </article>

        <footer>
            <?php $time=d ate(
            'l/d/m/y H:i:s');; echo $time;?> &nbsp &nbsp
            <?php echo "©";?>&nbsp &nbsp
            <?php echo
            "agadg"?>
        </footer>
    </div>
    <script src="js/app.js"></script>
</body>

</html>

app.js

var app = angular.module('myapp', []);
app.controller('mainController', ['$scope',
    function($scope) {
        $scope.pages = {
            name: 'glavna',
            key: 1
        };
    }
]);

我一直试图弄明白这一段时间,做了很多变化但没有成功:/这里有什么问题?提前谢谢。

5 个答案:

答案 0 :(得分:0)

关注Hello World! example

我猜正确的代码是

var app = angular.module('myapp', []);
app.controller('mainController', ['$scope',
  function($scope) {
  $scope.pages = { 
    name: 'glavna' ,
    key: 1
  };
}]);

您正在将模块初始化为&#39; app&#39;,因此您必须稍后使用此名称。不是&#39; appname&#39;。

答案 1 :(得分:0)

更改以下代码

var app = angular.module('myapp', []);
appname.controller('mainController', ['$scope',
function($scope) {
$scope.pages = { 
name: 'glavna' ,
key: 1
};
}]);

var app = angular.module('myapp', []);
app.controller('mainController', ['$scope',
function($scope) {
$scope.pages = { 
name: 'glavna' ,
key: 1
};
}]);

这可能对您有所帮助

答案 2 :(得分:0)

尝试包含以下内容:

<script src="js/app.js"></script>
头标记内的

答案 3 :(得分:0)

它确实有用......

&#13;
&#13;
var app = angular.module('myapp', []);
app.controller('mainController', ['$scope',
  function($scope) {
    $scope.pages = {
      name: 'glavna',
      key: 1
    };
  }
]);
&#13;
<!DOCTYPE html>
<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js">
  </script>
  <title>afa</title>
  <link rel="stylesheet" type="text/css" href="stil-index.css">
</head>

<body ng-app="myapp">
  <div class="flex-container" ng-controller="mainController">
    <header>
      <h1>{{pages.name}} </h1>
    </header>

    <article class="article">
      <h1>
        <?php echo "afgaga?" ?>
      </h1>
      <p>
        <?php echo "agasdga" ?>
      </p>
      <p><strong><?php echo "agsadga" ?></strong>
      </p>
    </article>

    <footer>
      <?php $time=d ate(
            'l/d/m/y H:i:s');; echo $time;?> &nbsp &nbsp
      <?php echo "©";?>&nbsp &nbsp
      <?php echo
            "agadg"?>
    </footer>
  </div>
  <script src="js/app.js"></script>
</body>

</html>
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

替换

//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js

https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js