在AngularJS中观察isolatebindings作为集合?

时间:2015-09-07 13:00:32

标签: angularjs angularjs-scope watch

我想知道是否有办法在我的指令中观察所有$$ isolateBindings?

$scope.$watch('$$isolateBindings', function() {
   console.log('watch me plse'); 
});

现在我这样做:

var isolateBindings = []

angular.forEach($scope.$$isolateBindings, function(item) {
    isolateBindings.push(item.attrName)
});



var watchIsValid = $scope.$watchGroup(isolateBindings, function() {
   console.log('do somthing'); 
});

1 个答案:

答案 0 :(得分:0)

我想它必须是

    namespace DataLayer.Repository
{
    public class CourseRepository
    {
        //Add Course
        public static void AddCourse(Course a)
        {
            //Create the SQL Query for inserting a Course
            string createQuery = String.Format("Insert Into Course(CourseName,Description,StartDate,EndDate,CourseMode) Values('{0}','{1}','{2}','{3}','{4}');" + "select @@Identity", a.CourseName, a.Description, a.StartDate.ToString("yyyy-mm-dd"), a.EndDate.ToString("yyyy-mm-dd"), a.CourseMode); 

            //Create and open a connection to SQL Server
            SqlConnection connection = DBManager.GetSqlConnection();

            //Create a Command object
            SqlCommand command = new SqlCommand("Insert Into Course" + "(CourseName,Description,StartDate,EndDate,CourseMode)" + "Values(@coursename,@description,@startdate,@enddate,@coursemode )", connection);

            command.Parameters.Add("@coursename", SqlDbType.NVarChar).Value = a.CourseName;
            command.Parameters.Add("@description", SqlDbType.NVarChar).Value = a.Description;
            command.Parameters.Add("@startdate", SqlDbType.DateTime).Value = a.StartDate;
            command.Parameters.Add("@enddate", SqlDbType.DateTime).Value = a.EndDate;
            command.Parameters.Add("@coursemode", SqlDbType.NVarChar).Value = a.CourseMode;

            try
            {
                //Execute the command to SQL Server and return Nothing
                var commandResult = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //there was a problem executing the script
            }

                //Close and dispose
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
        public static List<Course> GetCourse()
        {
            List<Course> CourseList = new List<Course>();

            //Create and open a connection to SQL Server
            SqlConnection connection = DBManager.GetSqlConnection();

            //Create a Command object
            SqlCommand command = new SqlCommand("Select CourseName, Description, StartDate, EndDate, CourseMode from Course", connection);

            try
            {
                //Execute the command to SQL Server and return A Reader
                SqlDataReader reader = command.ExecuteReader();
                //We could just use var CommandResult
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Course a = new Course();

                        a.ID = Convert.ToInt32(reader["ID"]);
                        a.CourseName = reader["CourseName"].ToString();
                        a.Description = reader["Description"].ToString();
                        a.StartDate = DateTime.Parse(reader["StartDate"].ToString());
                        a.EndDate = DateTime.Parse(reader["EndDate"].ToString());

                        }
                        a.CourseMode = (CourseModeOfDelivery)Enum.Parse(typeof(CourseModeOfDelivery), reader["CourseMode"].ToString());
                        CourseList.Add(a);
                        a = null;

                    }
                }
                else
                {
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                // without ex executing the script has problem
            }
            finally
            {
                // Close and Dispose 
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
            return CourseList;
        }

        //Get a Course by ID
        public static Course GetCourse(int id)
        {
            Course a = new Course();
            String GetCommand = "Select ID CourseName, Description, StartDate, EndDate, CourseMode from Course" + "Where ID = @CourseID";

            SqlConnection connection = DBManager.GetSqlConnection();
            SqlCommand command = new SqlCommand(GetCommand, connection);
            command.Parameters.AddWithValue("@CourseID", id);

            try
            {
                var reader = command.ExecuteReader();

                //Read the Command Object and then return details
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        a.ID = Convert.ToInt32(reader["ID"]);
                        a.CourseName = reader["CourseName"].ToString();
                        a.Description = reader["Description"].ToString();
                        a.StartDate = DateTime.Parse(reader["StartDate"].ToString());
                        a.EndDate = DateTime.Parse(reader["EndDate"].ToString());


                        switch (a.CourseMode)
                        {
                            case CourseModeOfDelivery.ClassRoom:
                                a.CourseMode = CourseModeOfDelivery.ClassRoom;
                                break;

                            case CourseModeOfDelivery.ELearning:
                                a.CourseMode = CourseModeOfDelivery.ELearning;
                                break;

                            case CourseModeOfDelivery.Online:
                                a.CourseMode = CourseModeOfDelivery.Online;
                                break;

                        }
                        a.CourseMode = (CourseModeOfDelivery) Enum.Parse(typeof(CourseModeOfDelivery),reader["CourseMode"].ToString());

                    }
                }
                else
                {
                    reader.Close();
                }
            }
            catch (Exception Ex)
            {

            }
            finally 
            {
                // Close and dispose
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
            return a;
        }


        //public static object GetCourses()
        //{
        //    throw new NotImplementedException();
        //}

         // Get All Course
        public static List<Course> GetCourses()
        {
            List<Course> CourseList = new List<Course>();

            //Create and open a connection to SQL Server 
            SqlConnection connection = DBManager.GetSqlConnection();

            //Create a Command object
            SqlCommand command = new SqlCommand("Select ID, CourseName, Description, StartDate, EndDate, CourseMode from Student", connection);


            try
            {
                //Execute the command to SQL Server and return A Reader 
                SqlDataReader reader = command.ExecuteReader();
                //or We Could just use var CommandResult
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Course a = new Course();
                        //
                        a.ID = Convert.ToInt32(reader["ID"]);
                        a.CourseName = reader["CourseName"].ToString();
                        a.Description = reader["Description"].ToString();
                        a.StartDate = DateTime.Parse(reader["StartDate"].ToString());
                        a.EndDate = DateTime.Parse(reader["EndDate"].ToString());
                        a.CourseMode =  (CourseModeOfDelivery)Enum.Parse(typeof(CourseModeOfDelivery),reader["CourseMode"].ToString());
                        // Add Object to List
                        CourseList.Add(a);
                        a = null;
                    }

                }
                else
                {
                    reader.Close();
                }


            }
            catch (Exception ex)
            {
                //there was a problem executing the script
            }
            finally
            {

                //Close and dispose
                command.Dispose();
                connection.Close();
                connection.Dispose();

            }
            return CourseList;

        }

        // Update Course
        public static int UpdateCourse(Course a)
        {
            int NumberOfRowsAffected = 0;
            string updateQuery = String.Format("Update Course SET CourseName=@coursename, Description=@description, StartDate=@startdate, EndDate=@enddate, CourseMode=@coursemode Where ID = @CourseID;"
                );
            SqlConnection connection = DBManager.GetSqlConnection();
            SqlCommand command = new SqlCommand(updateQuery, connection);
            command.Parameters.AddWithValue("@CoueseID", a.ID);
            command.Parameters.Add("@coursename",
                SqlDbType.NVarChar).Value = a.CourseName;
            command.Parameters.Add("@description",
                SqlDbType.NVarChar).Value = a.Description;
            command.Parameters.Add("@startdate",
                SqlDbType.SmallDateTime).Value = a.StartDate;
            command.Parameters.Add("@enddate",
                 SqlDbType.SmallDateTime).Value = a.EndDate;
            command.Parameters.Add("@coursemode",
                SqlDbType.NVarChar).Value = a.CourseMode;

            try
            {
                NumberOfRowsAffected = command.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                //there was a problem executing the script
            }

            //Close and dispose
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return NumberOfRowsAffected;
        }
        //Delete Student
        public static void DeleteCourse(int id)
        {

        }       
    }
}

这些是受监视的范围属性,而不是属性。

不太可能有另一种直接的方法来做到这一点。原因可能是有争议的,但这种方法足够好,不能再进一步了解。