通过服务在控制器和页面之间共享数据 - AngularJS

时间:2016-03-17 14:47:53

标签: javascript angularjs data-binding controller data-sharing

我是index.html的新人,我遇到了一个无法修复的问题。 我在我的应用程序的 <div class="col-sm-2"> <a href="text.html"> <img src="../icon-modify.png" id="img-mod-1"> </a> </div> 页面中,我让用户点击一个图标,以这种方式将他发送到另一个页面:

<textarea>

在第二页中,用户可以通过<textarea class="text-area-title-article allWidth ng-valid ng-touched" ng-controller="EditorController" rows="1" placeholder="Titolo articolo" ng-model="input.titlearticle2" > </textarea> <div class="col-sm-12 line-3"></div> <textarea class="text-area-subtitle allWidth ng-valid ng-touched" rows="1" placeholder="SOTTOTITOLO" ng-model="input.subtitlearticle2"> </textarea> <div class="row"> <div class="col-sm-12"> <a href="index.html"> <button type="button" class="btn btn-success" ng-click="setData(input)"> Success </button> </a> </div> </div> 标签插入标题和副标题;之后,他可以点击确认按钮并返回上一页,在那里他可以阅读刚插入另一个自定义模板的标题和副标题,这要归功于数据绑定。

 <article-2col titlearticle2="{{input.titlearticle2}}" subtitlearticle2="{{input.subtitlearticle2}}"</article-2col> 

这里是我想从第二页注入数据的第一页中的自定义元素:

testApp.controller('NewspaperController',
    function NewspaperController($scope, Article, $log) {  
        $scope.input = Article.getArticleObject(); 
        console.log($scope.input); 
    }

这里我有两个控制器(第一个是第一个控制器,第二个是第二个控制器):

NewspaperController.js

testApp.controller('EditorController',
    function EditorController($scope, Article, $log) {

        $scope.input = Article.getArticleObject(); 
        $scope.setData = function(ArticleObject){

        if (ArticleObject === undefined ) {
            console.log ("its undefined");
        } else {
            console.log (ArticleObject);
            Article.setArticleObject(ArticleObject);
        }

EditorController.js

Article.js

这是我的服务testApp.factory ('Article', function(){ article = {}; article.titlearticle2 = 'This is a title'; article.subtitlearticle2 = 'This is a subtitle'; return { setArticleObject: function (articleObject) { article = articleObject; }, getArticleObject: function () { return article; } } ,用于数据存储:

setData(input)

问题是方法object似乎不起作用,可能是因为我传递了错误的getArticleObject()? 此外,方法Private Sub GetMostRecentFile() Application.ScreenUpdating = False Application.DisplayAlerts = False Dim myFolder As Variant Dim myFileName As String Dim myFileDate As Date Dim myPartialLen As Integer myFilePath = "your folder path to search" myFileFindName = "Daily Global Model Extract" myDir = myFilePath If Right(myDir, 1) <> "\" Then myDir = myDir & "\" End If Set myFSO = CreateObject("Scripting.FileSystemObject") Set myFolder = myFSO.getfolder(myDir) myPartialLen = Len(myFileFindName) myFileDate = DateSerial(1900, 1, 1) For Each myFileObject In myFolder.Files x = Left(myFileObject.Name, myPartialLen) If x = myFileFindName Then Y = myFileObject.DateCreated If Y > myFileDate Then myFileDate = myFileObject.DateCreated myFileName = myFileObject.Name End If End If Next myFileObject If myFileName <> "" Then Workbooks.Open(myDir & myFileName) myMessage = "Opened file from " & myFileDate & "." Else myMessage = "Sorry, No files were found in this location." End If Application.DisplayAlerts = True Application.ScreenUpdating = True MsgBox myMessage, vbOKOnly, "Most Recent File" End Sub 工作正常,因为我使用服务初始化标题和副标题,它的工作原理。

希望你能帮助我。提前致谢!

0 个答案:

没有答案