Javascript 在AngularJS中将参数从视图传递到控制器到工厂

Javascript 在AngularJS中将参数从视图传递到控制器到工厂,javascript,angularjs,twitter-bootstrap,Javascript,Angularjs,Twitter Bootstrap,我对AngularJS使用bootstrap相当陌生。 我有以下HTML: <div ng-controller="BrandCompanyController"> <tabset> <tab ng-repeat="brand in brands" heading="{{brand.Name}}" active="brand.active" disable="brand.Disabled"> <div ng

我对AngularJS使用bootstrap相当陌生。 我有以下HTML:

<div ng-controller="BrandCompanyController">
    <tabset>
        <tab ng-repeat="brand in brands" heading="{{brand.Name}}" active="brand.active" disable="brand.Disabled">
            <div ng-controller="ModelController" ng-init="init(brand.ID)">

                <accordion>
                    <accordion-group heading="{{model.model_name}}" ng-repeat="model in models">
                        INSERT DATA HERE
                    </accordion-group>
                </accordion>


            </div>

        </tab>

    </tabset>
</div>
我的工厂:

myApp.factory('ModelFactory', ['$http', function ($http) {

    var ModelFactory = {};
    ModelFactory.GetModels = function (id) {

            return $http({
            method: 'GET',
            url: '/models/GetModels',
            params: { brandId: id }
        });
    };

    return ModelFactory;

}]);

ModelController中的$scope.init设置$scope.brandId。紧接着,在调用GetModels之前,$scope.brandId值是未定义的。我在这里做错了什么?

加载控制器后,将调用您的
$scope.init
。将
getModels($scope.brandId)
调用移动到该函数中:

$scope.init = function(id) {
    $scope.brandId = id;
    console.log("Inside Init: " + $scope.brandId);
    getModels(id);
}
这是因为首先,JS被加载并运行<代码>$scope.init已定义,
console.log(“外部init:+$scope.brandId”),并调用
getModels
函数。
然后,HTML完成加载。大约在这段时间,会执行
ng init=“init(brand.ID)”

在视图中使用此选项:

ng-click="functionName(model.id)"
控制器使用:

$scope.functionName = function(id) {
    restservice.post( '', "api/v1/.../?id=" +id).then(function(response) {
        if (response != null && response.success) {
            $scope.responseMessage = response.message;
        }
    });
}
$scope.functionName = function(id) {
    restservice.post( '', "api/v1/.../?id=" +id).then(function(response) {
        if (response != null && response.success) {
            $scope.responseMessage = response.message;
        }
    });
}