index.html中有多个ng-controller

时间:2017-04-30 10:18:49

标签: angularjs ionic-framework controller

This is my app.js  And I want add details to two tables .But when I run the code it only adds details to a one table.

这是为了向表medi10添加细节。     app.controller(“inforCtrl”,函数($ scope,$ cordovaSQLite){

        $scope.add = function(user){

            var query1 = "INSERT INTO medi10(drug,reason,dname)VALUES(?,?,?)";
            $cordovaSQLite.execute(db,query1,[user.drug,user.reason,user.dname])

      alert("added");

        }

    });

这是为表提醒4添加详细信息。      app .controller(“appCtrl”,function($ scope,$ cordovaSQLite){

        $scope.add = function(user){

            var query = "INSERT INTO reminder4(drug,time,how)VALUES(?,?,?)";
            $cordovaSQLite.execute(db,query,[user.dg,user.re,user.mel])

      alert("added");

        }

    })

1 个答案:

答案 0 :(得分:0)

不,不能,即使您拥有第一个也将被视为ng-controller。

<强>样本

var app = angular.module('myApp', []);
app.controller('myCtrl1', function($scope) {
  $scope.name = "Tom";
});
app.controller('myCtrl2', function($scope) {
  $scope.name = "Cruise";
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>AngularJS </title>
  <link rel="stylesheet" href="style.css" />
  <script src="https://code.angularjs.org/1.4.7/angular.js"></script>
 </head>
<body ng-app="myApp" ng-controller="myCtrl1" ng-controller="myCtrl2">
     {{name}}
</body>
</html>

如果你真的想在一个页面上有两个ng-controller,你可以通过分离div来实现。