首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在angularJS中创建可重用组件?

如何在angularJS中创建可重用组件?
EN

Stack Overflow用户
提问于 2014-11-19 20:26:34
回答 1查看 344关注 0票数 0

目前我有两个控制器,一个是用户输入值的表单,另一个是基于api搜索的图形。我需要根据来自第一个控制器的输入刷新或重新运行第二个控制器。我几乎可以肯定,这是一个糟糕的设计,但我还没有找到理想的方法去做。

HTML代码:

代码语言:javascript
复制
<html>
<body>


<!-- this is the graph generator-->
<div ng-controller="GraphAppCtrl"></div>

<!-- this is th form submittal section-->
<div ng-controller="FormCtrl" id="FormCtrl">

 <form novalidate class="simple-form">
    Name: <input type="text" ng-model="tech"/><br />

    <button ng-click="submit(tech)">Submit</button>
  </form>

</div>
</body>
</html>

角码:

代码语言:javascript
复制
var app = angular.module('testApp', []);


app.controller('FormCtrl', function ($scope) {

    $scope.submit = function(tech){

   // Need to be able to call the function that generates graph and pass it the tech value

     }
}

app.controller('GraphAppCtrl', function ($scope) {

//here I do a bunch of stuff to generate graph, like http request from third party api etc.


  }

现在发生了什么,因为我在我的index.html页面中引用了图形控制器,它第一次根据默认参数呈现,但是我需要它根据提交来呈现。

我看过指令,并在这个question中尝试了答案,但它似乎更像是在控制器之间传递数据的解决方案,而不是我想要做的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-19 20:45:28

您应该创建一个角服务

https://docs.angularjs.org/guide/services

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

https://stackoverflow.com/questions/27026340

复制
相关文章

相似问题

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