如何使用一个控制器在页面之间移动元素?

时间:2022-09-22 20:37:36

At a very abstract level (because the code is way too long to share here), I have a search box in an HTML file and I have a controller for that search box that does some things with the text entered in the text field. Now I want to pass the search results to a new HTML page just for the search results but for some reason this isnt working. I have the ng-controller directive in both HTML files and everything should be syntactically correct.

在一个非常抽象的层面(因为代码在这里分享的时间太长),我在HTML文件中有一个搜索框,我有一个搜索框的控制器,它使用在文本字段中输入的文本做一些事情。现在我想将搜索结果传递给新的HTML页面,仅用于搜索结果,但由于某种原因,这不起作用。我在两个HTML文件中都有ng-controller指令,一切都应该在语法上正确。

This worked fine when I used ng-include to include the second HTML file in the first one, but when I separated them I cant see the search results. I read somewhere on SO that separating the HTML pages as I have done creates a new $scope which is why when I use $scope.searchTerm in the controller only one of the HTML pages can see searchTerm, but I'm not sure if this is the problem.

当我使用ng-include将第二个HTML文件包含在第一个HTML文件中时,这很好用,但是当我将它们分开时,我无法看到搜索结果。我在某处读到了将HTML页面分开,因为我已经创建了一个新的$ scope,这就是为什么当我在控制器中使用$ scope.searchTerm时,只有一个HTML页面可以看到searchTerm,但我不确定这是不是是问题。

1 个解决方案

#1


0  

Use a service to store the value, that way the value can be read from the service into separate controllers. An Angular service is a singleton and is a great way to store data like that.

使用服务存储值,这样可以将值从服务读取到单独的控制器中。 Angular服务是一个单例,是存储这类数据的好方法。

The reason ng-controller is not working on a separate HTML file, is because it will re-load the controller, thus bringing it to its initial state, instead of the one after the edits you are hoping to pick up.

ng-controller不在单独的HTML文件上运行的原因是因为它会重新加载控制器,从而使其处于初始状态,而不是在您希望获取的编辑之后。

#1


0  

Use a service to store the value, that way the value can be read from the service into separate controllers. An Angular service is a singleton and is a great way to store data like that.

使用服务存储值,这样可以将值从服务读取到单独的控制器中。 Angular服务是一个单例,是存储这类数据的好方法。

The reason ng-controller is not working on a separate HTML file, is because it will re-load the controller, thus bringing it to its initial state, instead of the one after the edits you are hoping to pick up.

ng-controller不在单独的HTML文件上运行的原因是因为它会重新加载控制器,从而使其处于初始状态,而不是在您希望获取的编辑之后。