search

Home  >  Q&A  >  body text

angular.js - 当控制器的值发生改变时,如何及时将其传递给指令?

当我修改控制器controller内的某个变量时,希望指令directive能及时获取变量修改后的值。

app.directive('popoverMobile',function(){
    return {
        restrict:"E",
        transclude:true,
        scope:true,
        templateUrl:"tmpl/popover.mobile.tmpl.html",
        controller:["$scope",function($scope){
            $scope.popover_status=false;
            jQuery.ajax({
                type:"GET",                
                url:"https://**.***.com/**.htm?tel="+$scope.parents_detail.mobile,
                dataType:"jsonp",
                jsonp:"callback",
                jsonpCallback:"jsonpCallback",
                success:function(data){
                    $scope.mobile_info=data;
                }
            });
        }],
        link:function(scope){
            scope.switch_popover=function(val){
                scope.popover_status=val;
                scope.$apply();
            }
        },
        replace:true
    }
});

$scope.parents_detail.mobile 是控制器中的变量;
$scope.parents_detail.mobile 的值发生改变时,重新请求接口。

为情所困为情所困2744 days ago487

reply all(2)I'll reply

  • 黄舟

    黄舟2017-05-15 17:00:45

    $scope.$watch

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:00:45

    AngularJS directives can share attributes defined in the parent scope by default. For example, objects and attributes in the parent scope can be used directly in templates. Usually some simple directive functions can be implemented using this direct sharing method. When you need to create a reusable directive that only occasionally needs to access or modify the data of the parent scope, you need to use an isolated scope.

    AngularJS Directive isolates Scope data interaction

    reply
    0
  • Cancelreply