Javascript 角度指令和控制器双向绑定

Javascript 角度指令和控制器双向绑定,javascript,angularjs,Javascript,Angularjs,当试图从指令中更改控制器作用域变量时,出现上述错误。指令: Error: Expression 'undefined' used with directive is non-assignable 最后,我的指示是: angular.module('myApp') .controller('MainCtrl', function ($scope, $http) { $scope.selectedUser = {}; $scope.$watch('selectedUser',

当试图从指令中更改控制器作用域变量时,出现上述错误。指令:

Error: Expression 'undefined' used with directive is non-assignable
最后,我的指示是:

angular.module('myApp')
  .controller('MainCtrl', function ($scope, $http) {
    $scope.selectedUser = {};

    $scope.$watch('selectedUser', function() {
      console.log('user changed');
    });

});
如您所见,我正试图通过单击按钮来更改父(控制器)范围中的选定用户。但是当点击发生时,就会抛出错误


我已经看到了如何在指令和声明指令的html中定义范围的几种组合,但这些组合似乎都不起作用。我还缺少什么吗?

您的属性应该是
sel-user
,而不是
sel-user
,即
。投票结束作为一种排版补充:看看
angular.module('myApp')
  .controller('MainCtrl', function ($scope, $http) {
    $scope.selectedUser = {};

    $scope.$watch('selectedUser', function() {
      console.log('user changed');
    });

});
angular.module('myApp')
  .directive('mydir', function() {
    return {
      restrict: 'E',
      templateUrl: 'app/mydir/mydir.html',
      scope: {
        selUser: '='
      },
      link: function(scope) {
        scope.changeUser = function(user) {
          console.log('user: ' + JSON.stringify(user));
          scope.selUser = user;
        };
      }
    };
  });