在angularJs控制器中使用来自jquery方法的变量

时间:2015-04-02 10:41:25

标签: javascript jquery angularjs

我试图在更改的方法之外获取$ scope.selectedIndicator以在角度控制器中使用它。

关于如何获得$ scope.name的任何想法?对上述小提琴的评论也很受欢迎。

$scope.setSelectedIndicator = function () {
            $('#indicatorSelect').on('change', function(){
                $scope.selectedIndicator=$('#indicatorSelect').chosen().val();
                var res = $scope.selectedIndicator.split("|");
                $scope.selectedIndicatorId=res[0];
                $scope.selectedIndicatorName=res[1];
                console.log($scope.selectedIndicatorName);

                });
            $scope.name = $scope.selectedIndicatorName ;
        };
        $scope.setSelectedIndicator();

3 个答案:

答案 0 :(得分:0)

$适用于angularJS。您必须使用jQuery代替$。这是你的新功能:

        jQuery('#indicatorSelect').on('change', function(){
            $scope.selectedIndicator = jQuery('#indicatorSelect').chosen().val();
            var res = $scope.selectedIndicator.split("|");
            $scope.selectedIndicatorId=res[0];
            $scope.selectedIndicatorName=res[1];
            console.log($scope.selectedIndicatorName);

            });

答案 1 :(得分:0)

首先,您可以为此目的使用angular ng-change指令。最好避免角度内的jQuery,也只是为了从indicatorSelect获取所选值,jQuery不是必需的。你可以通过angular.element本身来做到这一点。

无论如何,在您的jQuery change事件处理程序中,您必须使用$scope.$apply()进行selectedIndicatorName中的更改,以反映在$scope.name中。

由于您不在角度上下文中,因此需要运行$scope.$apply()才能反映更改。

答案 2 :(得分:0)

$scope.setSelectedIndicator = function () {
            $scope.selectedIndicatorName='';
            jQuery('#indicatorSelect').on('change', function(){
                $scope.selectedIndicator = jQuery('#indicatorSelect').chosen().val();
                var res = $scope.selectedIndicator.split("|");
                $scope.Id=res[0];
                $scope.Name=res[1];
                $scope.$apply(function () {
                    $scope.selectedIndicatorName = $scope.Name;
                });
                console.log($scope.selectedIndicatorName);

            });