首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同角度app的角度服务

不同角度app的角度服务
EN

Stack Overflow用户
提问于 2015-07-24 09:45:17
回答 1查看 44关注 0票数 0

在这里,我使用的是角service.In,我的情况是,我得到了第一个应用程序的价值,而不是第二个.please帮助我。谢谢。

这是我的html:-

代码语言:javascript
复制
 <div ng-app="mainApp" ng-controller="CalcController">
      <p>Enter a number: <input type="number" ng-model="number" />
      <button ng-click="multiply()">X<sup>2</sup></button>
      <p>Result: {{result}}</p>
   </div>


        <div ng-app="myApp2" ng-controller="myController2">

      <p>Enter a number: <input type="number" ng-model="numberSecond" />
      <button ng-click="multiplyValue()">X<sup>2</sup></button>
      <p>Result: {{result2}}</p>


        </div>

这是js:-

代码语言:javascript
复制
angular.module('myReuseableMod',[]).factory('$myReuseableSrvc',function()
    {
    // code here
     var factory = {};
         factory.multiply = function(a)
         {
              return a * a
         }
                return factory;
      });



var mainApp = angular.module("mainApp", ['myReuseableMod']);
mainApp.controller('CalcController',['$scope', '$myReuseableSrvc',function($scope, $myReuseableSrvc) {
    alert("inside controller");
            $scope.multiply = function()
            {
                alert("hello1");
                   $scope.result = $myReuseableSrvc.multiply($scope.number);
             }
      }]);


var mainApp2 = angular.module("myApp2", ['myReuseableMod']);
mainApp.controller('myController2',['$scope', '$myReuseableSrvc',function($scope, $myReuseableSrvc) {
    alert("inside controller");
            $scope.multiplyValue = function()
            {
                alert("hello1");
                   $scope.result2 = $myReuseableSrvc.multiply($scope.numberSecond);
             }
      }]);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-25 01:48:20

您的“myController2”在错误的应用程序中

代码语言:javascript
复制
mainApp.controller('myController2'

应:

代码语言:javascript
复制
mainApp2.controller('myController2'

编辑:

啊,是的,我看到问题了。你不能像那样两次使用ng-app。如果您想要实现的是多个应用程序,那么必须“引导”第二个应用程序:

点击这里:http://plnkr.co/edit/qfllLO9uy6bC5OLkHnYZ?p=preview

代码语言:javascript
复制
angular.module('myReuseableMod',[]).factory('$myReuseableSrvc',function() {
  var factory = {};
  factory.multiply = function(a) {
    return a * a
  }

  return factory;
});


var mainApp = angular.module("mainApp", ["myReuseableMod"]);
mainApp.controller('CalcController', ['$scope', '$myReuseableSrvc',function($scope, $myReuseableSrvc) {

  $scope.multiply = function() {
    $scope.result = $myReuseableSrvc.multiply($scope.number);
  }

}]);

var mainApp2 = angular.module("mainApp2", []);
mainApp2.controller("MyController2", function($scope,  $myReuseableSrvc) {
  console.log('init B');
  $scope.multiplyValue = function() {
      $scope.result2 = $myReuseableSrvc.multiply($scope.numberSecond);
  }
});

angular.element(document).ready(function() {
    angular.bootstrap(document.getElementById("myDiv2"), ["mainApp2", "myReuseableMod"]);
});

这是一篇很好的文章:

http://www.simplygoodcode.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31621549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档