2016-10-01 21 views
0

我想從一個子指令更新控制器變量。 我更新了控制器變量,但這個值在視圖中不會改變。 需要我使用$ scope。$ apply()? $摘要?更新控制器變量,並在指令中查看使用它(角1.5)

這是我的代碼http://plnkr.co/edit/zTKzofwjPfg9eXmgmi8s?p=preview

JS文件

var app = angular.module('app', []); 

app.controller('parentController', function($scope) { 
    this.myVar = 'Hello from parent'; 

    this.refreshMyVar = function(data) { 
     this.myVar = data.name; 
     console.log('>> this.myVar', this.myVar); 
    }; 
}); 

app.directive('myDirective', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     template: '<input type="file" />', 
     bindToController: { 
      attrFromParent: '=' 
     }, 
     controller: 'directiveController as directiveCtrl', 
     link: function(scope, el, attr, ctrl) { 
      el.bind('change', function(e) { 
       ctrl.onChange(e.target.files[0]); 
      }); 
     } 
    }; 
}); 

app.controller('directiveController', function() { 
    this.onChange = function(file) { 
     this.attrFromParent(file); 
    }; 
}); 

HTML文件

<!DOCTYPE html> 
<html lang="en-US"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
<script src="app.js"></script> 
<body> 

<div ng-app="app" ng-controller="parentController as parentCtrl"> 
    <h1> >> {{parentCtrl.myVar}}</h1> 
    <p><my-directive attr-from-parent="parentCtrl.refreshMyVar" /></p> 
</div> 

</body> 
</html> 

如果你有其他的建議使我的乾淨,分享請

UPDATE

app.controller('parentController', function($scope) { 
    this.myVar = 'Hello from parent'; 

    this.refreshMyVar = data => { 
     this.myVar = data.name; 
     console.log('>> this.myVar', this); 
     $scope.$parent.$apply(); // solve my problem 
    }; 
}); 

$ scope。$ parent。$ apply();但我不是很滿意,如果有人有其他建議

+0

首先你寫了my-directive元素的語法錯誤。它不應該是一個自閉的元素。 –

回答

1

首先,你的my-directive元素的語法是錯誤的。它不應該是一個不是有效HTML的自閉元素。

語法應爲如下

<my-directive attr-from-parent="parentCtrl.refreshMyVar"></my-directive> 

第二和重要

app.controller('parentController', function($scope) { 
    var vm = this; 
    vm.myVar = 'Hello from parent'; 
    this.refreshMyVar = function(data) { 
     // You should not directly refer "this" inside refreshMyVar function 
     // since "refreshMyVar" function is executed in context of directive 
     // "this" will refer "myDirective" controller scope 
     // console.log("this :" this); // try to uncomment and see what "this" holds 
     // If you use "vm" you will be able to get hold of actual "myVar" due to 
     // closure formed by refreshMyVar function when it was created. 
     // console.log("vm :" vm); // try to uncomment and see what "vm" holds 
     // Reading about closures(JavaScript concept) will help you. 
     vm.myVar = data.name; 
     console.log(vm); 
     alert('>> this.myVar ' + vm.myVar); 
     // Since refreshMyVar is executed in context of directive, you have do 
     // $apply on parent scope to apply changes 
     $scope.$parent.$apply(); 
    }; 
}); 

工作plunker

article也可以幫助你。

+0

謝謝您的回答,我貼的代碼只是一個例子,但是當我的代碼,我使用ES6, ''' this.refreshMyVar =數據=> { \t this.myVar = data.name ; \t console.log('>> this.myVar',this); $ scope。$ parent。$ apply(); }; ''' So $ scope。$ parent。$ apply();解決我的問題 – JFouad

相關問題