【问题标题】:angular ionic real simple controller not working角度离子真正简单的控制器不起作用
【发布时间】:2015-09-29 10:19:51
【问题描述】:

我是 angularjs 的新手。我正在尝试创建一个简单的控制器,但我做不到。 真的需要帮助。

<html data-ng-app="ionicApp" >

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Ionic Template</title>
    <script src="lib/ionic/js/ionic.bundle.js"></script>                                            
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>

    <script>
      function SimpleController($scope){
                              $scope.customers=[
                                          {name:'Djo', city:'johannesburg'},
                                          {name:'tri',city:'vaal'},
                                          {name:'stone',city:'pretoria'},
                                          {name:'loick',city:'durban'}
                                        ];
                             }

                </script>
<body >
    <div class="container" ng-controllers="SimpleController">   
       <ul >
           <!-- we are accessing thescope -->
            Name: <input type="text" data-ng-model="name" /> {{ name}} 
           <li ng-repeat="cust in customers"> {{ custt.name}} - {{cust.city}} </li>
       </ul>  
    </div>  
   </body>
</html>

【问题讨论】:

标签: javascript android html angularjs ionic


【解决方案1】:

全局控制器在 angularjs 1.3 + 中被禁用

<html ng-app="ionicApp">
<body>
<div ng-controller="SimpleController">

</div>
</body>
<script>
     angular.module("ionicApp",[])
            .controller("SimpleController", [$scope, function($scope){
                 // your code here
            }])
</script>

【讨论】:

  • 在 angularjs ver 1.3.0 中使用 ng-controller 时出错,先生也无法正常工作
  • 我真的无法弄清楚错误是什么。在浏览器上,它只显示文本框 {{name}} 和底部的 {{cust.name}}{{cust.city}},就像它们出现在代码中一样。这真的很奇怪,因为通常你们给我的所有解决方案都应该有效......
  • @trinidado 请检查您的萤火虫控制台是否有错误
  • 它仍然告诉我范围没有定义。不知道你
  • 我的错,把这行.controller("SimpleController", [$scope, function(){改成.controller("SimpleController", [$scope, function($scope){
【解决方案2】:

解决了!就这么简单:

而不是

<script>
     angular.module("ionicApp",[])
            .controller("SimpleController", [$scope, function(){
                 // my code here
            }])
</script>

我只是将 $scope 作为函数中的参数传递了

<script>
     angular.module("ionicApp",[])
            .controller("SimpleController",function($scope){
                 // my code here
            })
</script>

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2015-06-02
    相关资源
    最近更新 更多