2015-10-05 95 views
1

我有一個部分使用一個名爲CaseNotesCtrl的控制器。我在訪問這部分內部的$ scope變量時遇到問題。該代碼是這樣的:

<div class="row" ng-show="$parent.loggedin" ng-controller="CaseNotesCtrl"> 
    <div class="col-sm-12 note 
-field" ng-show="$parent.addingNote"> 
     <label for="noteEntry">Enter your note</label> 
     <textarea class="form-control" name="noteEntry" rows="4" ng-model="newNote.note"></textarea> 
     <br /> 
     <a href="" class="btn btn-xs btn-danger calicon-view note-save-button" ng-click="saveCaseNotes(note.Case.CaseID, note.User.UserID, note.NoteID)" data-toggle="tooltip" data-placement="top" title="Save Note" tooltip><span class="glyphicon glyphicon-floppy-save"></span></a> 
     <a href="" data-toggle="tooltip" data-placement="top" title="Cancel Adding Note" class="btn btn-xs btn-danger calicon-view note-cancel-button" ng-click="$parent.cancelNote();" tooltip> 
      <span class="glyphicon glyphicon-remove-sign"></span> 
     </a> 
    </div> 
    <div class="col-sm-12"> 
     <a href="" data-toggle="tooltip" data-placement="top" title="Add a Note" class="btn btn-xs btn-danger calicon-view note-add-button" ng-click="$parent.addNote();" ng-show="!$parent.addingNote" tooltip> 
      <span class="glyphicon glyphicon-list-alt"></span> 
     </a> 
    </div> 
    <div class="col-sm-12 note-list-heading" ng-repeat-start="note in caseNotes"> 
     <span class="note-header">Note entered by {{ note.User.DisplayName }} and was last edited on {{ note.ModifiedDate | date: "MM/dd/yyyy 'at' h:mma" }}</span><a href="" data-toggle="tooltip" data-placement="top" title="Edit This Note" class="btn btn-xs btn-danger calicon-view pull-right note-edit-button" ng-click="editNote(note.Case.CaseID, note.NoteID, note.Notes)" tooltip><span class="glyphicon glyphicon-edit"></span></a> 
    </div> 
    <div class="col-sm-12 note-text"> 
     {{ note.Notes }} 
    </div> 
    <div ng-repeat-end></div> 
</div> 

這裏是控制器代碼:

JBenchApp.controller('CaseNotesCtrl', ['$scope', '$http', '$routeParams', 'HoldState', function ($scope, $http, $routeParams, HoldState) { 

    // Management of case notes 
    $scope.NotesCaseID = $routeParams.number; 
    $scope.NotesUserName = localStorage.getItem('UserName'); 
    $scope.NotesUserRole = localStorage.getItem('UserRole'); 
    $scope.addingNote = false; 

    $scope.newNote = { note: 'Testing 123', CaseID: 0, NoteID: 0 }; 

    $scope.getCaseNotes = function (CaseID, UserName, UserRole) { 
     $http.get('http://10.34.34.46/BenchViewServices/api/CaseNote/' + CaseID + "/" + UserRole + "/" + UserName).success(function (response) { 
      $scope.caseNotes = response; 
     }) 
    }; 

    $scope.saveCaseNotes = function (CaseID, UserName, NoteID) { 

     $scope.addingNote = false; 
    }; 

    $scope.addNote = function() { 
     $scope.addingNote = true; 
    }; 

    $scope.cancelNote = function() { 
     $scope.newNote.note = ''; 
     $scope.addingNote = false; 
    }; 

    $scope.editNote = function(caseID, noteID, noteText){ 
     $scope.newNote.note = noteText; 
     $scope.newNote.CaseID = caseID; 
     $scope.newNote.NoteID = noteID; 
     $scope.addingNote = true; 
     $parent.addingNote = true; 
    }; 

    $scope.getCaseNotes($scope.NotesCaseID, $scope.NotesUserName, $scope.NotesUserRole); 


}]); 

正如你可以看到我有在地方,如$parent.addingNote使用$parent。如果我將其更改爲$scope.addingNote,則不會調用該函數。 $parent唯一的地方就是$parent.isLoggedIn。我該如何解決這個問題,以便它只使用$scope

+0

你在哪裏定義了$ scope.loggedin? – Nicolas2bert

+0

在另一個名爲JBenchCtrl的控制器中 –

+0

有沒有鏈接btw這2個控制器(JBenchCtrl&CaseNotesCtrl)? – Nicolas2bert

回答

0

我建議您在使用ng-controller時使用AngularJS「Controller as」語法。

在這種情況下,它會是這樣的:

app.controller('CaseNotesCtrl', function() { 
    this.title = 'Some title'; 
}); 


<div ng-controller="CaseNotesCtrl as caseNotes"> 
    {{ caseNotes.title }} 
</div> 

所以,你會很容易知道哪個範圍與Google合作。

相關問題