Method rendering in scope fails

I have a set of directives that share scope

Update:

The code is available as plunk here http://plnkr.co/edit/JZ77bsZgGrw6N4K718Is?p=preview

ToDo element:

 app.directive("todoItem",function(DeleteTodo,$log){
var dirDefObj = {
    restrict:'E',
    templateUrl:'app/templates/todo.html',
    scope:{
        todo:'=value'
    },
    controller:function($scope){},
    replace:true
};
return dirDefObj;
});

      

template:

<div class="ui card">
  <todo-formui ng-show="todo.editMode"></todo-formui>
  <todo-cardui ng-show="!todo.editMode"></todo-cardui>
</div>

      

There are two directives that inherit the scope of this directive:

ToDo-Cardui

app.directive('todoCardui',function(){
var dirDefObj = {
    restrict:'E',
    templateUrl:'app/templates/display-todo.html',
    scope:false,
    replace:true,
    controller:function($scope)
    {
        $scope.clickDone = function clickDone(){
            //two tasks (1)toggle the done value on the todo (2) toggle the btnText on the todo
            $scope.todo.done = !$scope.todo.done;
            $scope.todo.btnText = $scope.todo.done?'Reinstate':'Done';
        };

        $scope.remove = function remove()
        {
            DeleteTodo.delete($scope.todo);
            $scope.$emit('todo:deleted',$scope.todo);
        };

        $scope.edit = function edit(value)
        {
            $scope.todo.editMode = true;
            $scope.savedState = angular.extend({},$scope.todo);
        };  
    }
};
return dirDefObj;
});

      

template:

 <div>
<div class="content">
    <i class="right floated blue pencil icon" ng-click="edit()"></i> 
    <header class="ui medium header">
        <span ng-class="{'done-todo':todo.done,'normal-todo':!todo.done}">{{todo.task}}</span>
    </header>
    <div class="description">
        <p>{{todo.description}}</p>
    </div>
</div>
<div class="extra content">
    <button class="ui small green toggle button floated left" ng-click="clickDone()">{{todo.btnText}}</button>
    <button class="ui small red button floated left" ng-click="remove()">Delete</button>
</div>
</div>

      

ToDo-formui:

app.directive("todoFormui",function(EditService){
var dirDefObj = {
    restrict:'E',
    templateUrl:'app/templates/edit-todo.html',
    scope:false,
    controller:function($scope){
        $scope.display = function display(){
            console.log("Inside the edit to preview function");
            $scope.editMode = false;
        };

        $scope.save = function(){
            EditService.edit($scope.todo);
        };

        $scope.discard = function(){
            $scope.todo={
                task:'',
                dscription:'',
                btnText:''
            };
            $scope.todo = $scope.savedState;

        };
    },
    replace:true
};
return dirDefObj;
});

      

template:

<div ng-class="{description:show_modal,content:!show_modal}">
<i class="right floated blue unhide icon" ng-click="display()"></i>
<form class="ui small form">
    <div class="field">
        <label>Task</label>
        <input type="text" name="task" placeholder="What do you want to do?"ng-model="todo.task">
    </div>
    <div class="field">
        <label>Description</label>
        <textarea ng-model="todo.description"></textarea>
    </div>
    <div class="two fields">
        <button class="ui red button floated right">Discard</button>
        <button class="ui submit green button floated right" ng-click="save()">Save</button>
    </div>  
</form>

      

While executing the code, I found that the display function in todo-formui would fail no matter where I put it or what I tried to do. The function of saving to the same volume works without problems.

+3


source to share


2 answers


In your preview function (as mentioned in plunker) you need to update the scope as

$ scope.todo.editMode = false;

instead



$ scope.editMode = false;

then preview will be available

Hope it helps

+1


source


This is because the icon is under another html element and the click event is not fired. Add a message after the Preview icon (which floats) to push the form where it should be. Use some DOM inspectors and you will soon figure out the problem.



+1


source







All Articles