为什么我的按钮只能从特定的URL工作

时间:2015-05-02 05:07:50

标签: javascript html angularjs angular-directive

我的按钮位于两个部分模板中,完全相同。但它不适用于主页模板。

我想让函数deleteNote()工作。当我在特定音符的url按钮工作时,但当我在家庭模板时它就不会。有人知道为什么?

note.html

<div class="col-lg-3" ng-controller="mainCtrl">
    <div class="list-group" ng-repeat="note in notes">
        <a href="#note/{{note.id}}"   class="list-group-item" style="max-height: 90px; overflow: hidden">
            {{note.title}}
            <p style="text-align: justify;"  ><small>{{note.body}}</small></p>
        </a>
        <button  ng-click="deleteNote(note)" class="btn btn-default btn-xs" style="float: right;">Delete</button>
    </div>
</div>

home.html与您看到的完全相同

<div class="col-lg-3" ng-controller="mainCtrl">
    <div class="list-group" ng-repeat="note in notes">
        <a href="#note/{{note.id}}"   class="list-group-item" style="max-height: 90px; overflow: hidden">
            {{note.title}}
            <p style="text-align: justify;"  ><small>{{note.body}}</small></p>
        </a>
        <button  ng-click="deleteNote(note)" class="btn btn-default btn-xs" style="float: right;">Delete</button>
    </div>
</div>

notesCtrl.js

angular.module('theNotesApp')
    .controller('notesCtrl', [
        '$scope',
        'notesFactory',
        'note',
        '$stateParams',
         '$state',  function($scope, notesService, note, $stateParams, $state) {
            $scope.note = note;
            $scope.title = note.title;
            $scope.body = note.body;
            $scope.updateNote = function() {
                notesService.update($stateParams.id, {title: $scope.title, body: $scope.body});
                notesService.getAll();
            };

            $scope.deleteNote = function(note) {
                notesService.delete(note.id);
                $state.go('home');
                notesService.getAll();
            };

        }])

mainCtrl.js

.controller('mainCtrl',['$scope', 'notesFactory', function($scope, notesService){
    $scope.notes = notesService.notesObjectInService;

    notesService.getAll();

    $scope.addNote = function(){
        if ($scope.title === "" ) {
            return;
        }
        notesService.create({
            title: $scope.title,
            body:$scope.body
        });
        $scope.title= '';
        $scope.body= '';
    };
}])

1 个答案:

答案 0 :(得分:0)

deleteNote的定义是notesCtrl不在mainCtrl的范围。您需要在mainCtrl上创建方法deleteNote。这是demo