简体   繁体   English

angular.js指令双向绑定范围更新

[英]angular.js directive two-way-binding scope updating

I wanted to use a directive to have some click-to-edit functionality in my front end. 我想使用指令在前端具有一些点击编辑功能。 This is the directive I am using for that: http://icelab.com.au/articles/levelling-up-with-angularjs-building-a-reusable-click-to-edit-directive/ 这是我为此使用的指令: http : //icelab.com.au/articles/levelling-up-with-angularjs-building-a-reusable-click-to-edit-directive/

'use strict';

angular.module('jayMapApp')
  .directive('clickToEdit', function () {
    return {
      templateUrl: 'directives/clickToEdit/clickToEdit.html',
      restrict: 'A',
      replace: true,
      scope: {
        value: '=clickToEdit',
        method: '&onSave'
      },
      controller: function($scope, $attrs) {
        $scope.view = {
          editableValue: $scope.value,
          editorEnabled: false
        };

        $scope.enableEditor = function() {
          $scope.view.editorEnabled = true;
          $scope.view.editableValue = $scope.value;
        };

        $scope.disableEditor = function() {
          $scope.view.editorEnabled = false;
        };

        $scope.save = function() {
          $scope.value = $scope.view.editableValue;
          $scope.disableEditor();
          $scope.method();
        };
      }
    };
  });

I added a second attribute to the directive to call a method after when the user changed the value and then update the database etc. The method (´$onSave´ here) is called fine, but it seems the parent scope is not yet updated when I call the method at the end of the directive. 我在指令中添加了第二个属性,以在用户更改值后更新数据库等之后调用方法。方法(此处为“ $ onSave”)称为“罚款”,但似乎父范围尚未更新。我在指令末尾调用该方法。 Is there a way to call the method but have the parent scope updated for sure? 有没有一种方法可以调用,但是可以确定父范围是否已更新?

Thanks in advance, Michael 预先感谢迈克尔

I believe you are supposed to create the functions to attach inside the linking function: 我相信您应该创建要附加在链接功能内的功能:

Take a look at this code: 看一下这段代码:
http://plnkr.co/edit/ZTx0xrOoQF3i93buJ279?p=preview http://plnkr.co/edit/ZTx0xrOoQF3i93buJ279?p=preview

app.directive('clickToEdit', function () {
    return {
      templateUrl: 'clickToEdit.html',
      restrict: 'A',
      replace: true,
      scope: {
        value: '=clickToEdit',
        method: '&onSave'
      },
      link: function(scope, element, attrs){
        scope.save = function(){
          console.log('save in link fired');
        }
      },
      controller: function($scope, $attrs) { 
        $scope.view = {
          editableValue: $scope.value,
          editorEnabled: false
        };

        $scope.enableEditor = function() {
          $scope.view.editorEnabled = true;
          $scope.view.editableValue = $scope.value;
        };

        $scope.disableEditor = function() {
          $scope.view.editorEnabled = false;
        };

        $scope.save = function() {
          console.log('save in controller fired');
          $scope.value = $scope.view.editableValue;
          $scope.disableEditor();
          $scope.method();
        };
      }
    };
  });

I haven't declared the functions inside the controller before, but I don't see why it wouldn't work. 我以前没有在控制器中声明过函数,但是我不明白为什么它不起作用。

Though this question/answer explain it Link vs compile vs controller 尽管此问题/答案对其进行了解释链接vs编译vs控制器

From my understanding: 据我了解:
The controller is used to share data between directive instances, not to "link" functions which would be run as callbacks. 控制器用于在指令实例之间共享数据,而不是“链接”将作为回调运行的功能。

The method is being called but angular doesn't realise it needs to run the digest cycle to update the controller scope. 该方法正在被调用,但是angular没有意识到它需要运行摘要循环来更新控制器范围。 Luckily you can still trigger the digest from inside your isolate scope just wrap the call to the method: 幸运的是,您仍然可以从隔离范围内触发摘要,只需包装对方法的调用即可:

$scope.$apply($scope.method());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM