AngularJS:当变量变为指令时触发控制器函数

时间:2015-07-20 10:28:42

标签: javascript angularjs

我有一个指令要做两件事:(i)它管理HTML中某些标签的可视化,(ii)它function checkForm(){ document.getElementById("frm1").addEventListener("submit", function(e) { var errors = []; //Validate first name: Required, Alphabetic (no numbers) if(document.getElementById("firstname").value === "") { document.getElementById("errFname").style.display = "inline"; document.getElementById("firstname").focus(); errors.push("required"); }//close if var alphaRegEx = /^[a-zA-Z]+$/;; var alphafname = document.getElementById("firstname").value; var alphalname = document.getElementById("lastname").value; //check if first name has any digits if (!alphaRegEx.test(alphafname)){ document.getElementById("errFname").style.display = "inline"; document.getElementById("firstname").value=""; document.getElementById("firstname").focus(); errors.push("numeric"); }//close if //check if last name has any digits if (!alphaRegEx.test(alphalname)){ document.getElementById("errLname").style.display = "inline"; document.getElementById("lastname").value=""; document.getElementById("lastname").focus(); errors.push("numeric"); }//close if //If you want, you can do something with your errors, if not, just return if (errors.length > 0) { e.preventDefault(); return false; } return true; });//close function }//close function (checkForm) window.onload=checkForm; es 对象的值{{1在会话存储中,将此对象保存在名为$watch的变量中。这里有代码:

myObj

每次更改时,我都有一个控制器来调用WS传递上述myData的属性.directive('panelManager', function(){ return { restrict: 'E', templateUrl: 'foo.html', controller: ['$scope', '$window', function($scope, $window) { pn = this; pn.tab = 1; // init this.selectTab = function(setTab) { pn.tab = setTab; }; this.isSelected = function(checkTab) { return pn.tab === checkTab; }; $scope.$watch(function () { return $window.sessionStorage.getItem('myObj'); }, function (value) { pn.myData = JSON.parse(value); }); }], controllerAs: 'panels' }; }) 。这是:

foo

有没有办法在该控制器中触发myData并在每次myData更改值时将.controller('MyDataController', ['$http', function($http) { pdc = this; pdc.myResultset = []; pdc.getDetails = function(val) { return $http.get("myUrl"+val+"/").then(function(response){ pdc.myResultset= response.data; }); }; }]); 传递给它? 通过这种方式,我可以在getDetails中存储WS的当前结果集并将其分配给本地范围。

1 个答案:

答案 0 :(得分:2)

您可以从指令发出,但需要关联控制器中的$rootScope。声明成为:

controller: ['$rootScope', '$scope', '$window',  function($rootScope, $scope, $window) {

然后,你可以这样做:

$scope.$watch(function () {
    return $window.sessionStorage.getItem('myObj');
}, function (value) {
    pn.myData = JSON.parse(value);
    $rootScope.$broadcast('valueChanged', pn.myData);
});

在你的控制器中:

.controller('MyDataController', ['$http', '$scope', function($http, $scope) {

    pdc = this;
    pdc.myResultset = [];

    pdc.getDetails = function(val) {
        return $http.get("myUrl"+val+"/").then(function(response){
            pdc.myResultset= response.data;
        });
    };

    $scope.$on('valueChanged', function(event, data) {
        pdc.getDetails(data.foo); //pass data.foo to the function
    });
}]);